LaunchAgent - 将参数传递给应用程序

时间:2015-08-03 22:22:21

标签: applescript launch-agent

我有一个LaunchAgent,当用户登录时会调用一个应用程序。该应用程序会加载一个网站。

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>

应用

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

如何将网站参数从LaunchAgent传递到应用程序?

1 个答案:

答案 0 :(得分:0)

你可以用它。第二个参数(在可执行文件之后)应为&#34; - &#34;然后第三个应该是&#34; http://www.apple.com&#34;或者你想要的任何东西:

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
编辑:哦,是的,我弄清楚为什么AppleScript没有识别&#34;显示警报&#34;。您必须声明&#34;使用脚本添加&#34;在导入框架时启用完整的AppleScript ..