Suggestions for making this faster (Asynchronous?)

Geniuses,

I have an app-launching script which I am fairly happy with except it’s slow. Anyone got suggestions for making it faster?

Kel said, try putting ‘ignoring application responses’ inside the main tell block, but that didn’t work. Viewing the error message that emitted, I decided to put the instruction around the ‘activate’, which when I thought about it is probably where the whole thing was bogging down. It certainly SEEMS faster. So cheers to Kel.

This a pretty handy function, actually.

--
--	Created by: Lorin Rivers
--	Created on: 02/10/14 10:25:38
--
--	AppleScript App Launcher by Lorin Rivers is licensed under an
--  MIT License.
--  
--  Pass the application name to the function appLauncher
--  This AppleScript will request your admin password because it writes
--  to the system log. Other than that, it should be completely harmless.
--  
--  provide a list of applications you want to launch in this form:
--  set launchThis to my appLauncher("Google Chrome Canary")
--  

set launchThis to my appLauncher("LaunchBar")

set launchThis to my appLauncher("Default Folder X")

set launchThis to my appLauncher("Mail")

set launchThis to my appLauncher("BusyCal")

set launchThis to my appLauncher("TimeKeeper")

set launchThis to my appLauncher("Yojimbo")

set launchThis to my appLauncher("Xmarks for Safari")

on appLauncher(appName)
	
	-- call System Events to discover if an app is running or not
	tell application "System Events"
		
		-- if there are no apps named appName running
		if (count (every process whose name is appName)) = 0 then
			
			-- write to the system log. If the admin password request
			-- is a concern, comment out every "do shell script" line
			do shell script "logger " & appName & " is not running" with administrator privileges
			
			-- ask Finder to launch the app named appName
			tell application "Finder"
				try
					tell my application appName
						ignoring application responses							
							activate
						end ignoring
					end tell
					
					-- write to the system log
					do shell script "logger starting " & appName with administrator privileges
					
					-- something bad happened	
				on error errMsg number errNum
					do shell script "logger The errors are: " & errMsg & ": " & errNum with administrator privileges
				end try
			end tell
			
			-- app named appName is running
		else
			do shell script "logger " & appName & " is already running" with administrator privileges
		end if
	end tell
end appLauncher

Hi lrivers,

You can try placing ‘ignoring application responses’ at the beginning of the System Events tell block and an ‘end ignoring’ at the end of the tell block. Not sure if anything will happen with those ‘do shell script’ lines. You can test it.

gl,
kel

Kel,

Definitely put me on the right track. I put the ‘ignoring’ bits outside the ‘activate’ and it certainly SEEMS faster. More testing will have to ensue.

Usually my questions don’t get answered because they don’t make sense. Clearly, I have leveled up in AppleScript.

Thanks!

Another thing is instead of counting the process with a name, you can use ‘exists’. Like:

tell application "System Events"
	exists process "Safari"
end tell

gl,
kel

You can simplify that considerably:

set launchThis to my appLauncher("com.barebones.yojimbo")

on appLauncher(appId)
	try
		tell application id appId to set isRunning to running
		if not isRunning then
			do shell script "logger " & appName & " is not running" with administrator privileges
			tell application id appId to run
			do shell script "logger starting " & appName with administrator privileges
		else
			do shell script "logger " & appName & " is already running" with administrator privileges
			
		end if
	on error errMsg number errNum
		do shell script "logger The errors are: " & errMsg & ": " & errNum with administrator privileges
	end try
end appLauncher

By using the id instead of the name, errors don’t result in the “choose application” dialog.

Without logging but running completely in the background:

launchApp("iTunes")
launchApp("Mail")
launchApp("iCal")
launchApp("Safari")

on launchApp(appName)
	do shell script "$(ps -Aco command | grep -i '^'" & quoted form of appName & "'$' || open -a " & quoted form of appName & ") &>/dev/null &"
end launchApp

Wow! That’s heavy.