Application launching function, with example & logging to the console.

This is something I use every day. I don’t want all my apps opening at launch and I like an uncluttered dock. I have several of these scripts for task-specific batch application launching.

Also has a console logging feature which is kind of handy too.

DJ Bazzy Wazzy suggested I increase the security of the do shell script call with tell current application to. So I did that too.

--
--	Created by: Lorin Rivers
--	Created on: 6/5/14, 6:17 PM
--
--	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 myName to ""

--tell application "System Events" to set myName to get name of (path to me)

set launchThis to my appLauncher("Google Chrome")

set launchThis to my appLauncher("Skype")

on appLauncher(appName)
	
	-- call System Events to discover if an app is running or not
	tell application "System Events"
		set myName to get name of (path to me)
		
		-- 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 "tell current application to do shell script" line
			tell current application to do shell script "logger " & myName & ": " & 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
					tell current application to do shell script "logger " & myName & ": " & "starting " & appName with administrator privileges
					
					-- something bad happened	
				on error errMsg number errNum
					tell current application to do shell script "logger " & myName & ": " & "The errors are: " & errMsg & ": " & errNum with administrator privileges
				end try
			end tell
			
			-- app named appName is running
		else
			tell current application to do shell script "logger " & myName & ": " & appName & " is already running" with administrator privileges
		end if
	end tell
end appLauncher

You have do shell script commands inside other tell application blocks than itself. This violates the scripting addition security and should only be used in the current application context.

So how could I both avoid violating the security AND get the console writing results I want?

You can prefix the do shell script lines with a tell current application:


tell application "Finder"
	tell current application to do shell script "echo 'Hello World!'"
end tell

or execute the do shell in a handler and call the handler from inside an tell application block:

tell application "Finder"
	my simpleHandler("Hello World!")
	--or
	simpleHandler("Hello World!") of me
end tell

on simpleHandler(arg1)
	do shell script "echo " & quoted form of arg1
end simpleHandler

and last but not least you can exit the tell application block temporarily and open it again:

tell application "Finder"
	--do your finder stuff
end tell --targeting to current application again

do shell script "echo 'Hello World!'"

tell application "Finder"
	--continue doing things in the Finder
end tell

OK, made that change. Why is this more secure? Just trying to improve my skills.

My nickname is MC Silverback. Maybe we should go on tour.

When you run your script and show the event log you’ll see errors (-10004) and a retry of the do shell script in another context. The script is working but it’s better to have no errors at all.

Maybe we should indeed, I’m more a producer than a DJ at this moment.