LaunchAgent - Pass Parameters to Application

434 Views Asked by At

I have a LaunchAgent which invokes an application when the user logs in. The application loads a website.

LaunchAgent

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>LaunchAgent.Test</string>
    <key>RunAtLoad</key>
    <true/>
    <key>ProgramArguments</key>
    <array>
    <string>/Applications/MyApp.app/Contents/MacOS/applet</string>
    </array>
</dict>
</plist>

Application

display alert set_cookies_for_URL("http://www.apple.com")
(* AppleScript's handlers all seem to become unusable after importing frameworks.
 * To compensate, I'm relegating AppleScript/ObjC calls to the end of the file
 *)
use framework "WebKit"

on set_cookies_for_URL(URL)

    set web_view to current application's WebView's alloc()'s initWithFrame:(current application's NSMakeRect(0, 0, 500, 500)) frameName:"tempFrame" groupName:"tempGroup"
    set web_view's mainFrameURL to URL
    #delay to avoid release
    repeat while ((web_view's isLoading) as boolean) is true
        delay 1
    end repeat
    return "done"
end set_cookies_for_URL

How would I go about passing the website parameter into the application from the LaunchAgent?

1

There are 1 best solutions below

0
On

You can use this. The second argument (after the executable) should be "-" then the third should be "http://www.apple.com" or whatever you want it to be:

use framework "WebKit"
use scripting additions

set argv to (current application's NSProcessInfo's processInfo()'s arguments as list)
if (count of argv) is 3 then
    set_cookies_for_URL(item 3 of argv)
end if

quit

on set_cookies_for_URL(URL)
    set web_view to current application's WebView's alloc()'s initWithFrame:(current application's NSMakeRect(0, 0, 500, 500)) frameName:"tempFrame" groupName:"tempGroup"
    set web_view's mainFrameURL to URL
    #delay to avoid release
    repeat while ((web_view's isLoading) as boolean) is true
        delay 1
    end repeat
end set_cookies_for_URL

EDIT: Oh yes, and I figured out why AppleScript wasn't recognizing "display alert". You have to declare "use scripting additions" to enable full AppleScript when importing a framework..