Search code examples
macosbashshellapplescriptautomator

OSX: How can check whether a file exists in current directory using applescript?


I want to make an automator app which creates an empty file in current directory. I did some google search and found:
http://hints.macworld.com/article.php?story=20050219134457298 and http://hints.macworld.com/article.php?story=20100509134904820

However, I want to do something more powerful. If the specified file already exists, I want to show a warning instead of overwriting the original file, which is what one of the above link does. (The other one creates a text file using textEdit. I do not want to create text file. I want an empty file like what linux/unix does)

I already figured out how to do most of the part, but

  1. How can check whether a file exists in current directory using applescript??
  2. How can I concatenate two variable in applescript?

Solution

  • Checking if a file exists (assuming thefullpath is already set as in the referenced question):

    tell application "Finder"
       if exists POSIX file thefullpath then
            --do something here like
            display alert "Warning: the file already exists"
       end if
    end tell
    

    Not sure what you mean by the second part but if you want to concatenate strings stored in var1 and var2 you could simply do

    var1 & var2