2016-10-04 125 views
0

我想写一个可以创建.plist文件并将其移动或复制到文件夹“LaunchAgents”的applescript。 当我尝试通过拖放来做到这一点时,我需要对其进行身份验证 - 我认为这是为什么我的代码无法正常工作的问题。用苹果创建一个新的plist文件夹“LaunchAgents”

首先我尝试这样做:

做shell脚本 “触摸〜/库/ LaunchAgents/COM。” & “NameOfTheFile” & “的.plist”

但是,这是行不通的。我不知道为什么......而且我没有得到一个错误。 所以我试过这个,因为我认为它是因为认证:

做shell脚本“touch〜/ Library/LaunchAgents/com”。 &“NameOfTheFile” &“的.plist”用户名‘MyUserName输入’密码‘MyPassword输入’与 管理员权限

不过这也没有工作,所以我想我在我的第一行代码做到这一点:

做shell脚本 “搭配chmod 777〜/库/ LaunchAgents” 用户名 “MyUserName输入” 密码 “MyPassword输入” 以管理员权限

也不行! :/

所以我的问题是: 我的命令是否错误? 为什么它不起作用? 如何才能在AppleScript文件夹“LaunchDaemon”或“LaunchAgents”中创建一个新文件?

回答

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:不要在(~)/Librarychmod文件夹,除非你知道自己在做什么。

相关问题