2016-10-04 33 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文件夾,除非你知道自己在做什麼。

相關問題