使用AppleScript检查前桌面窗口路径是否在Desktop文件夹中

时间:2012-02-18 07:58:03

标签: objective-c macos 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的新手,必须犯一些非常基本的错误。有人可以告诉我哪里出错了吗?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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
相关问题