ping localhost
ping localhost

Reputation: 479

Check if the front desktop window path is inside the Desktop folder with AppleScript

I have an issue with an applescript I have written. The script needs to check if the front desktop window path is inside the Desktop folder (i.e a prefix match.) This is the script:

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

However on executing it gives the error :

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

I am a total newbie with Applescript and must be making some very basic mistake. Can someone please tell me where I am going wrong?

Upvotes: 1

Views: 217

Answers (2)

dj bazzie wazzie
dj bazzie wazzie

Reputation: 3542

Well because it seems that my edits on adayzdone isn't accepted or anything I will post it in a new answer. His problem is that he does a string comparison with non string objects and therefore it doesn't work. It should be something like this:

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

Upvotes: 1

adayzdone
adayzdone

Reputation: 11238

Try this:

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

Upvotes: 0

Related Questions