2012-02-18 29 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

相关问题