只使用applescript创建一个新的plist文件夹" LaunchAgents"

时间:2016-10-04 11:20:31

标签: macos shell terminal operating-system applescript

我想编写一个可以创建.plist文件并将其移动或复制到文件夹" LaunchAgents"的AppleScript。 当我尝试通过拖放操作执行此操作时,我需要对其进行身份验证 - 我认为这是我的代码无效的问题。

首先我尝试了这个:

  

做shell脚本"触摸〜/ Library / LaunchAgents / com。" &安培; " NameOfTheFile" &安培;   "的.plist"

但这不起作用。我不知道为什么......而且我没有收到错误。 所以我尝试了这个因为我认为它是因为身份验证:

  

做shell脚本"触摸〜/ Library / LaunchAgents / com。" &安培; " NameOfTheFile"   &安培; "的.plist"用户名" MyUsername"密码" MyPassword"同   管理员权限

但这也没有用,所以我想我在代码的第一行做了这个:

  

做shell脚本" chmod 777~ / Library / LaunchAgents"用户名   "名为myUsername"密码" MyPassword"具有管理员权限

也不行! :/

所以我的问题是: 我的命令错了吗? 为什么不起作用? 如何在文件夹" LaunchDaemon"中创建一个新文件?或" LaunchAgents"只用applecript?

1 个答案:

答案 0 :(得分:0)

尝试这样的事情,将属性列表简单地创建为文字文本,然后写入目录。将ProgramProgramArguments更改为您的值。

property pListLabel : "com.myLabel"

set userLibraryPath to path to library folder from user domain as text
set LaunchAgentsFolder to userLibraryPath & "LaunchAgents:"
set pListfile to LaunchAgentsFolder & pListLabel & ".plist"
try
    tell application "Finder" to make new folder at folder userLibraryPath with properties {name:"LaunchAgents"}
end try

set PLIST_data to "<?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>" & pListLabel & "</string>
    <key>LowPriorityIO</key>
    <true/>
    <key>Program</key>
    <string>/usr/bin/mycommand</string>
    <key>ProgramArguments</key>
    <array>
        <string>my</string>
        <string>argument</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>
"
try
    set ff to open for access file pListfile with write permission
    write PLIST_data to ff as «class utf8»
    close access ff
on error
    try
        close access file target
    end try
    display dialog pListLabel & " couldn't be created" buttons {"Cancel"} default button 1
end try

PS:chmod中不要(~)/Library个文件夹,除非您确切知道自己在做什么。

相关问题