Shell Script Troubles

This code is obviously not the prettiest thing right now but I will change that later…
My problem is that the shell script I am using works fine in apple script but when I transfer it over to AppleScriptObjC it runs until the shell part and then just freezes the program.
Anyone have any ideas why? My .xib is correct so that is not the problem.

script SimpleAppAppDelegate
	property parent : class "NSObject"
    
    -- IBOutlets
    property phoneNumber : missing value
    property messageSubject : missing value
    
    on buttonClick_(sender)
        set qChar to ASCII character 34
        
        set numberX to phoneNumber's stringValue()
        
        set subjectX to messageSubject's stringValue()
            
        (choose from list {"AT&T", "Sprint", "T-Mobile", "Verizon", "US Cellular"} ¬
        with prompt "What is their carrier")
        set carrierW to result as text
        if carrierW = "AT&T" then
            set carrierX to "txt.att.net"
            else if carrierW = "Verizon" then
            set carrierX to "vtext.com"
            else if carrierW = "Sprint" then
            set carrierX to "messaging.sprintpcs.com"
            else if carrierW = "T-Mobile" then
            set carrierX to "tmomail.net"
            else if carrierW = "US Cellular" then
            set carrierX to "email.uscc.net"
        end if
        
            do shell script "mailx -s " & qChar & subjectX & qChar & " " & numberX & "@" & carrierX
        
        
        
        end buttonClick_
	
	on applicationWillFinishLaunching_(aNotification)
		-- Insert code here to initialize your application before any files are opened 
	end applicationWillFinishLaunching_
	
	on applicationShouldTerminate_(sender)
		-- Insert code here to do any housekeeping before your application quits 
		return current application's NSTerminateNow
	end applicationShouldTerminate_
	
end script

Hi,

the script behaves the same way in Script Editor or Xcode: it waits until the shell script has finished.
As AppleScript is single-threaded you have (at least) three options
¢ redirect the result of the shell script to dev/null, this causes the code line after the shell script to continue immediately

  • Use the asynchronous skills of NSTask
  • Use the method performSelectorInBackground, the method’s name explains itself.

Here is a solution for the last option


script SimpleAppAppDelegate
	property carrierNames : {"AT&T", "Sprint", "T-Mobile", "Verizon", "US Cellular"}
	property carrierIdentifiers : {"txt.att.net", "messaging.sprintpcs.com", "tmomail.net", "vtext.com", "email.uscc.net"}
	property parent : class "NSObject"
	
	-- IBOutlets
	property phoneNumber : missing value
	property messageSubject : missing value
	
	on buttonClick_(sender)
		set numberX to phoneNumber's stringValue()
		set subjectX to messageSubject's stringValue()
		
		set chosenCarrier to (choose from list carrierNames with prompt "What is their carrier")
		if chosenCarrier is false then return
		set chosenCarrier to item 1 of chosenCarrier
		repeat with i from 1 to (count carrierNames)
			if item i of carrierNames is chosenCarrier then
				set carrierX to item i of carrierIdentifiers
				exit repeat
			end if
		end repeat
		set commandString to "mailx -s " & quote & subjectX & quote & " " & numberX & "@" & carrierX
		my performSelectorInBackground_withObject_("performShellScriptInBackground:", commandString)
	end buttonClick_
	
	on applicationWillFinishLaunching_(aNotification)
		-- Insert code here to initialize your application before any files are opened 
	end applicationWillFinishLaunching_
	
	on applicationShouldTerminate_(sender)
		-- Insert code here to do any housekeeping before your application quits 
		return current application's NSTerminateNow
	end applicationShouldTerminate_
	
	on performShellScriptInBackground_(object)
		do shell script (object as string)
	end performShellScriptInBackground_
end script