2012-02-18 35 views
1

我遇到了我編寫的applescript的問題。該腳本需要檢查前的桌面窗口的路徑是桌面文件夾內(即前綴匹配)。這是腳本:使用AppleScript檢查前臺桌面窗口路徑是否位於Desktop文件夾內

tell application "Finder" 
    set currentPath to (POSIX path of (target of front window as alias)) 
end tell 
set thecommandstring to "echo \"" & currentPath & "\" | grep -q \"^/Users/host/Desktop/\" " 
set grepResult to do shell script thecommandstring 

但是在執行它給人的錯誤:

error "The command exited with a non-zero status." number 1 

我是AppleScript的新手,必須犯一些非常基本的錯誤。有人能告訴我我要去哪裏嗎?

回答

1

很好,因爲它似乎是我的adayzdone的編輯不被接受或任何東西,我將它張貼在一個新的答案。他的問題是他與非字符串對象進行字符串比較,因此它不起作用。它應該是這樣的:

tell application "Finder" 
    set desktopPath to path to desktop as string 
    set windowPath to (target of the front window as string) 
    if windowPath begins with desktopPath then 
     return true 
    else 
     return false 
    end if 
end tell 
+0

我不想發佈更新,直到明天我回到我的電腦前對其進行測試。謝謝您的幫助。 – adayzdone 2012-02-20 02:34:48

+0

它完美的作品..謝謝! – 2012-02-20 09:12:00

0

試試這個:

tell application "Finder" 
set desktopPath to path to desktop 
set windowPath to (target of the front window as alias) 
if windowPath contains desktopPath then 
    beep 
end if 
end tell 
+0

請注意,作爲文件系統引用,別名不僅僅是路徑,例如即使該項目被重命名或移動,它仍然可以工作。要檢查路徑,您需要將別名強制爲文本或以其他方式解析,以便進行比較。 – 2012-02-18 18:21:00

+0

不包含該帳戶的帳戶?你有錯誤嗎? – adayzdone 2012-02-18 23:08:11

+0

沒有錯誤(只要有Finder窗口),但如果Finder窗口的目標是桌面,您的腳本將只會得到一個匹配。例如,如果目標是桌面上的另一個文件夾,即使目標的路徑包含桌面的路徑,也不會匹配。爲了得到理想的結果,路徑需要被解析以進行比較(除非我誤解了這個問題)。 – 2012-02-19 00:26:21

相關問題