Checking for existence of hidden files in AppleScript

I can check to see if a file exists using AppleScript. But I am having trouble with hidden files. Is there a special way to do this?

set home_path to (path to home folder as text)
set hiddenFile to home_path & ".test"

tell application "Finder"
    if not (exists file hiddenFile) then
        display dialog ".test does not exist."
    end if
end tell

Replace Finder with System Events:

set home_path to (path to home folder as text)
set hiddenFile to home_path & ".test"

tell application "System Events"
    if exists file hiddenFile then
        display dialog ".test does exist."
    else
        display dialog ".test does not exist."
    end if
end tell

and it works.


You can also inline calling the shell command inside the AppleScript without the need to create separate bash scripts. Here's an example:

set home_path to (path to home folder as text)
set fileName to ".viminfo"
set hiddenFile to home_path & fileName

tell application "Finder"
    if not (exists file hiddenFile) then
        try
            set output to do shell script "[ -f " & quoted form of POSIX path of hiddenFile & " ];"
        on error
            display dialog fileName & " does not exist."
        end try
        display dialog fileName & " does exist."
    end if
end tell

This AppleScript uses bash [ -f $PATH ]; to check if file exists. It also uses try, because you have to "catch" an error - if file do not exist you get an error and script is stopped.


So I don't know how to do this with native AppleScript, but because Macs use Bash, you can use a shell script to do the searching. It's not ideal but it's better than nothing.

So your shell script is going to cd, change directory, into the folder you need to search. Then, it'll execute the ls command, which lists the contents of a directory. the -a flag on ls tells it to list hidden items. Bash tools use a concept called standard input/output to get input and display output. The standard output is displayed on screen by default, but it can be redirected by a tool called a pipe to go somewhere else. In this case, instead of displaying a list of all the files in a directory, we're going to pipe that list into a search tool called grep. That explanation was somewhat verbose but your shell script only needs 3 lines (the first of which is arguably unnecessary in this case):

#!/bin/bash                                   # define the shell, not really needed because bash is the default shell but good practice
cd /path/to/search/directory                  # cd to the proper place
ls -a | grep "the text you're looking for"    # list all files (including hidden ones) and pipe output into grep, a search tool

Save this as something logical, like findFile.sh, then make it executable with this command in the terminal app

chmod +x /path/to/findFile.sh

So now that's ready to go, on to the AppleScript.

Like I said I'm not that experienced in AppleScript, but as I understand it you'd run the shell script like this

set output to do shell script "/path/to/findFile.sh"

then the output variable would contain any text that grep found. Your test for whether or not the file exists is now whether or not output contains any text.

Feel free to run man grep in the terminal for the grep manual to see more about how it works and what it matches and how to only match files with certain extensions and stuff like that. Perhaps you may want to play around piping things into grep to get a feel for how it works.

Hopefully someone can come along with an answer to your question in AppleScript.

References:

  • using shell scripts in AppleScript: http://macscripter.net/viewtopic.php?id=4913
  • chmod is required to make shell scripts executable: How to run a shell script from an AppleScript?
  • for any of the shell commands, run man nameofcommand to learn more about them