Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I have an applescript application that inputs a username and starts a download based on that username. In the app I use the code below to check if a file exists already and then rename the file if it does.

tell application "Finder"
if exists "~/Downloads/Conversion/" & cbUsername & ".flv" as POSIX file then
    set x to 1
    repeat
        set newCbFilename to cbUsername & "_" & x as string
        if exists "~/Downloads/Conversion/" & newCbFilename & ".flv" as POSIX file then
            set x to x + 1
        else
            exit repeat
        end if
    end repeat
    copy newCbFilename to finalCbFilename
    display dialog "Filename already exists " & "File will be named: " & finalCbFilename & ".flv" buttons "OK" default button "OK" with title "Error" with icon caution
else
    copy cbUsername to finalCbFilename
end if
end tell

All of a sudden yesterday it stopped working correctly. I had added the following code to ensure that the folder I was saving to existed.

tell application "System Events"
if not (exists folder "~/Downloads/Conversion") then
    do shell script "mkdir ~/Downloads/Conversion"
end if

Even when I comment out that code now it still doesn't work. What did I do wrong? end tell

share|improve this question

1 Answer 1

up vote 2 down vote accepted

It looks like the Finder needs the absolute path to the home folder instead of the relative path. Instead of starting the path with ~/, it needs to start with /Users/username/.

Instead of hardcoding the username into the script, you can have AppleScript build the absolute path on the fly:

set homePath to POSIX path of (path to home folder)

Then you can replace "~/ with homePath & "

For example:

if exists "~/Downloads/Conversion/" & cbUsername & ".flv" as POSIX file then

would become

if exists homePath & "Downloads/Conversion/" & cbUsername & ".flv" as POSIX file then

Alternatively, if you only use ~ with the path ~/Downloads/Conversion/, you could instead change that whole path to a variable:

set cbPath to POSIX path of (path to home folder) & "Downloads/Conversion/"

Then the final script would be:

set cbPath to POSIX path of (path to home folder) & "Downloads/Conversion/"

tell application "Finder"
    if exists cbPath & cbUsername & ".flv" as POSIX file then
        set x to 1
        repeat
            set newCbFilename to cbUsername & "_" & x as string
            if exists cbPath & newCbFilename & ".flv" as POSIX file then
                set x to x + 1
            else
                exit repeat
            end if
        end repeat
        copy newCbFilename to finalCbFilename
        display dialog "Filename already exists " & "File will be named: " & finalCbFilename & ".flv" buttons "OK" default button "OK" with title "Error" with icon caution
    else
        copy cbUsername to finalCbFilename
    end if
end tell
share|improve this answer
1  
Alright, I'll try this. I'm just curious why it decided to stop working now is all. It worked fine up until I added the code I mentioned. O_o –  Eli Greenberg Apr 4 '12 at 6:55
    
Well it works great so thanks for the help. So bizarre! –  Eli Greenberg Apr 4 '12 at 10:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.