Skip to main content
added 61 characters in body
Source Link
historystamp
  • 1.9k
  • 11
  • 19

One way would be to check for the existence of the files before you attempt the rename. Write a loop. Start where your default start file name would be. Run the names until find an untaken name.

It's harder than it should be to find out if a file exists or does not exist. This routine while long has worked for me. To avoid this handler, copy the code into your check loop.:

-- ------------------------------------------------------  
(*
  ideas from:
  https://stackoverflow.com/questions/3469389/applescript-testing-for-file-existence
  
  
 use the alias way.  Others ways ran into trouble eventually. 
*)
on fileExists(theFile) -- (String) as Boolean
    (* "System Events" and "Finder" checking for file existance revealed problems. l*)
    set debugging to false
    if debugging then log "  fileExists: theFile is " & theFile
    try
        set theAlias to theFile as alias
        set theExistance to true
    on error errMsg number n
        if debugging then log "  fileExists: n is " & n
        -- File or folder doesn't exist. 
        if n is not -43 then
            set commonError to "on error the errMsg is " & errMsg & " number is " & n
            if debugging then log "  fileExists: " & commonError
            display dialog commonError giving up after 10
            -- cause grief above.
            error "Failure of alias." number -1
        else
            set theExistance to false
        end if
    end try
    if debugging then log "  fileExists: theExistance is " & theExistance
    return theExistance
end fileExists

One way would be to check for the existence of the files before you attempt the rename. Write a loop. Start where your default start file name would be. Run the names until find an untaken name.

It's harder than it should be to find out if a file exists or does not exist. This routine while long has worked for me:

-- ------------------------------------------------------  
(*
  ideas from:
  https://stackoverflow.com/questions/3469389/applescript-testing-for-file-existence
  
  
 use the alias way.  Others ways ran into trouble eventually. 
*)
on fileExists(theFile) -- (String) as Boolean
    (* "System Events" and "Finder" checking for file existance revealed problems. l*)
    set debugging to false
    if debugging then log "  fileExists: theFile is " & theFile
    try
        set theAlias to theFile as alias
        set theExistance to true
    on error errMsg number n
        if debugging then log "  fileExists: n is " & n
        -- File or folder doesn't exist. 
        if n is not -43 then
            set commonError to "on error the errMsg is " & errMsg & " number is " & n
            if debugging then log "  fileExists: " & commonError
            display dialog commonError giving up after 10
            -- cause grief above.
            error "Failure of alias." number -1
        else
            set theExistance to false
        end if
    end try
    if debugging then log "  fileExists: theExistance is " & theExistance
    return theExistance
end fileExists

One way would be to check for the existence of the files before you attempt the rename. Write a loop. Start where your default start file name would be. Run the names until find an untaken name.

It's harder than it should be to find out if a file exists or does not exist. This routine while long has worked for me. To avoid this handler, copy the code into your check loop.:

-- ------------------------------------------------------  
(*
  ideas from:
  https://stackoverflow.com/questions/3469389/applescript-testing-for-file-existence
  
  
 use the alias way.  Others ways ran into trouble eventually. 
*)
on fileExists(theFile) -- (String) as Boolean
    (* "System Events" and "Finder" checking for file existance revealed problems. l*)
    set debugging to false
    if debugging then log "  fileExists: theFile is " & theFile
    try
        set theAlias to theFile as alias
        set theExistance to true
    on error errMsg number n
        if debugging then log "  fileExists: n is " & n
        -- File or folder doesn't exist. 
        if n is not -43 then
            set commonError to "on error the errMsg is " & errMsg & " number is " & n
            if debugging then log "  fileExists: " & commonError
            display dialog commonError giving up after 10
            -- cause grief above.
            error "Failure of alias." number -1
        else
            set theExistance to false
        end if
    end try
    if debugging then log "  fileExists: theExistance is " & theExistance
    return theExistance
end fileExists
Source Link
historystamp
  • 1.9k
  • 11
  • 19

One way would be to check for the existence of the files before you attempt the rename. Write a loop. Start where your default start file name would be. Run the names until find an untaken name.

It's harder than it should be to find out if a file exists or does not exist. This routine while long has worked for me:

-- ------------------------------------------------------  
(*
  ideas from:
  https://stackoverflow.com/questions/3469389/applescript-testing-for-file-existence
  
  
 use the alias way.  Others ways ran into trouble eventually. 
*)
on fileExists(theFile) -- (String) as Boolean
    (* "System Events" and "Finder" checking for file existance revealed problems. l*)
    set debugging to false
    if debugging then log "  fileExists: theFile is " & theFile
    try
        set theAlias to theFile as alias
        set theExistance to true
    on error errMsg number n
        if debugging then log "  fileExists: n is " & n
        -- File or folder doesn't exist. 
        if n is not -43 then
            set commonError to "on error the errMsg is " & errMsg & " number is " & n
            if debugging then log "  fileExists: " & commonError
            display dialog commonError giving up after 10
            -- cause grief above.
            error "Failure of alias." number -1
        else
            set theExistance to false
        end if
    end try
    if debugging then log "  fileExists: theExistance is " & theExistance
    return theExistance
end fileExists