Alex
Alex

Reputation: 2976

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?

Upvotes: 1

Views: 5568

Answers (4)

markhunte
markhunte

Reputation: 6932

Something I have been using a lot of late for this sort of thing is the command /bin/test The test test for the existence of in this case a file

    if (do shell script "/bin/test -e " & quoted form of  (POSIX path of theFile)  & " ; echo $?") is "1" then
-- 1 is false
--do something

end if

The -e option:

-e file True if file exists (regardless of type).

The are tons of other test options shown in the /bin/test man page

Upvotes: 1

Lri
Lri

Reputation: 27613

Another option that doesn't require Finder or System Events is to try to coerce a POSIX file or file object to an alias:

try
    POSIX file "/tmp/test" as alias
    true
on error
    false
end try

Upvotes: 0

Kaydell
Kaydell

Reputation: 474

The following code, adapted from your second link, is usually right, but it doesn't always work. The current directory is better specified as the directory of the document that is being opened which is most likely from the Finder's front window, but not necessarily. I like to write code that will work no matter what.

on run {input, parameters}
    tell application "Finder"
        set currentPath to insertion location as text
        set x to POSIX path of currentPath
        display dialog "currentPath: " & (x as text)
    end tell
    return x
end run

I wrote a whole "Run AppleScript" action to put things into context:

on run {input, parameters}

    # count the number of files
    set numFiles to 0
    repeat with f in input
        # warn the user that folders are not processed in this app
        tell application "Finder"
            if (kind of f is "Folder") then
                display dialog "The item: " & (f as text) & " is a folder.  Only files are allowed. Do you want to continue processing files or do you want to cancel?"
            else
                set numFiles to numFiles + 1
            end if
        end tell
    end repeat

    # require that at least one file is being opened
    if numFiles < 1 then
        display alert "Error: the application Test1.app cannot be run because it requires at least one file as input"
        error number -128
    end if

    # get the current directory from the first file
    set theFirstFile to (item 1 of input)
    tell application "System Events" to set theFolder to (container of theFirstFile)

    # ask the user for a file name
    set thefilename to text returned of (display dialog "Create file named:" default answer "filename")

    # create the file
    tell application "System Events" to set thefullpath to (POSIX path of theFolder) & "/" & thefilename
    set theCommand to "touch \"" & thefullpath & "\""
    do shell script theCommand

    # return the input as the output
    return input

end run

The "touch" command is OK. If the file doesn't exist, it is created and if it does exist, only the modification date is changed (which isn't too bad) but it doesn't overwrite the file. If your file is being overwritten, it's not the touch command that is doing it.

I changed the default file name to remove the extension ".txt" This extension may default to being opened by TextEdit.app, but you can change this in the Finder by choosing "Get Info" for a file and changing the "Open With" property. You can change which application opens the file with that extension or you can change them all. For example, all of my ".txt" files are opened with BBEdit.app

Will you vote my answer up?

Upvotes: 0

kamjagin
kamjagin

Reputation: 3654

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

Upvotes: 3

Related Questions