Thursday, May 23, 2019

#1 2010-07-30 04:54:13 am

regulus6633
Member
From:: Taulov, Denmark
Registered: 2006-11-02
Posts: 1695
Website

Launch login items for all users at startup

Over at MacOSXHints I came across an interesting post, provided a solution, and thought you might like to see it too.

The basic question is how do you run startup items for every user? There is a link to this website which provides a shell script and methodology for doing that. Basically it tells you to create a plist in /Library/Preferences/ to run the shell script. What I did was convert that shell script into an applescript... and improved it a little. Here's the applescript and if you're interested you can read about creating the plist file at that website.

Any improvements are welcome! big_smile

Applescript:

-- this applescript will launch any shell scripts, applescripts, or applications placed inside it
-- you can also place alias's to any of those in the application bundle
--
-- Created by Hank McShane on 30 July 2010
-- [url]http://www.hamsoftengineering.com[/url]
--
-- I just basically converted the shell script found at the below website into applescript code
-- and added the ability to also run applescript bundles (scptd files). Look at the following
-- link for a methodology of running this applescript at startup for all users.
-- [url]http://www.macenterprise.org/articles/runningitemsatlogin[/url]
--
-- TO USE:
-- save this code as an application bundle
-- create the folder "Contents:Resources:LaunchItems:" inside the application bundle
-- place items you want launched in that folder

global myName

on run
   set launchItemsPath to (path to me as Unicode text) & "Contents:Resources:LaunchItems:"
   set myName to getMyName()
   
   logToSystemLog("Started Running login items...")
   try
       -- make sure the folder exists and continue script if it does
       alias launchItemsPath
       
       tell application "Finder"
           set launchItems to (files of folder launchItemsPath) as alias list
       end tell
       
       repeat with anItem in launchItems
           try
               -- if its executable then we execute it
               set isExecutable to do shell script "if [ -x " & quoted form of POSIX path of anItem & " -a ! -d " & quoted form of POSIX path of anItem & " ]; then echo 'true';fi"
               if isExecutable is "true" then
                   logToSystemLog("Executing: " & POSIX path of anItem)
                   do shell script quoted form of POSIX path of anItem & " &"
               else
                   -- if it's not executable we determine its kind
                   tell application "Finder" to set theKind to kind of (contents of anItem)
                   --return theKind
                   if theKind is "Alias" then
                       tell application "Finder" to set theKind to kind of (original item of (contents of anItem))
                   end if
                   
                   -- we launch it based on its kind
                   if theKind begins with "script" or theKind begins with "compiled script" then
                       logToSystemLog("Running Applescript: " & POSIX path of anItem)
                       run script (contents of anItem)
                   else
                       logToSystemLog("Opening: " & POSIX path of anItem)
                       --tell application "Finder" to launch (contents of anItem)
                       do shell script "/usr/bin/open " & quoted form of POSIX path of (contents of anItem)
                   end if
               end if
           on error theError number errorNumber
               set errorText to "Error: " & theError & " Error Number: " & errorNumber
               logToSystemLog(errorText)
           end try
       end repeat
   on error theError number errorNumber
       set errorText to "Error: " & theError & " Error Number: " & errorNumber
       logToSystemLog(errorText)
   end try
   logToSystemLog("Finished Running login items...")
end run

on logToSystemLog(theText)
   do shell script "/usr/bin/logger " & quoted form of (myName & space & theText)
end logToSystemLog

on getMyName()
   set myPath to path to me as Unicode text
   if myPath ends with ":" then
       set n to -2
   else
       set n to -1
   end if
   set AppleScript's text item delimiters to ":"
   set myName to text item n of myPath
   if (myName contains ".") then
       set AppleScript's text item delimiters to "."
       set myName to text 1 thru text item -2 of myName
   end if
   set AppleScript's text item delimiters to ""
   return myName
end getMyName

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)