Sunday, July 22, 2018
  • Index
  •  » Code Exchange
  •  » Application launching function, with example & logging to the console.

#1 2014-06-04 03:00:41 pm

lrivers
Member
From:: Austin, TX
Registered: 2005-07-19
Posts: 6
Website

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.

Applescript:

--
--    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

Last edited by lrivers (2014-06-05 05:17:48 pm)


Balding, greying nerd, dad, motorcyclist, volunteer. Still have a NeXT in my attic. And a Newton.

Offline

 

#2 2014-06-05 03:03:31 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2778
Website

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

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.

Offline

 

#3 2014-06-05 12:43:09 pm

lrivers
Member
From:: Austin, TX
Registered: 2005-07-19
Posts: 6
Website

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

DJ Bazzie Wazzie wrote:

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?


Balding, greying nerd, dad, motorcyclist, volunteer. Still have a NeXT in my attic. And a Newton.

Offline

 

#4 2014-06-05 04:12:14 pm

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2778
Website

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

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

Applescript:


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:

Applescript:

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:

Applescript:

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

Last edited by DJ Bazzie Wazzie (2014-06-05 04:12:28 pm)

Offline

 

#5 2014-06-05 05:19:25 pm

lrivers
Member
From:: Austin, TX
Registered: 2005-07-19
Posts: 6
Website

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

DJ Bazzie Wazzie wrote:

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.


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.


Balding, greying nerd, dad, motorcyclist, volunteer. Still have a NeXT in my attic. And a Newton.

Offline

 

#6 2014-06-06 02:28:02 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2778
Website

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

lrivers wrote:

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


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.

lrivers wrote:

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


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

Offline

 
  • Index
  •  » Code Exchange
  •  » Application launching function, with example & logging to the console.

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)