Back to your original code.
If I run a similar code on local volume with a non available destination folder:
--set myItem to POSIX file "/Volumes/Server/Directory/To/MyFolder"
--set destinationFolder to POSIX file "/Volumes/Server/Directory/To/DestinationFolder"
set myItem to (path to desktop as string) & "pour Musée"
set destinationFolder to (path to documents folder as string) & "unavailableDestinationFolder"
tell application "Finder"
set newItem to duplicate myItem as alias to folder destinationFolder
end tell
return newItem as alias
The script enter what resemble to an infinite loop and I must relaunch the Finder to exit it.
May I assume that you got the described error when the target folder is unavailable ?
It would not be the first time where we receive an error message which doesn’t match the real problem.
For local volumes, I would be forced to use an enhanced script:
--set myItem to POSIX file "/Volumes/Server/Directory/To/MyFolder"
--set destinationFolder to POSIX file "/Volumes/Server/Directory/To/DestinationFolder"
set myItem to (path to desktop as string) & "pour Musée"
set destinationFolder to (path to documents folder as string) & "unavailableDestinationFolder"
tell application "Finder"
if exists folder destinationFolder then
set newItem to duplicate myItem as alias to folder destinationFolder
else
error "the folder “" & destinationFolder & "” is not available"
end if
end tell
return newItem as alias
Given the way items are defined in your script it would be useful to edit your script as :
set myItem to POSIX file "/Volumes/Server/Directory/To/MyFolder"
set destinationFolder to POSIX file "/Volumes/Server/Directory/To/DestinationFolder"
tell application "Finder"
if exists destinationFolder then
set newItem to duplicate myItem to destinationFolder
else
error "The folder “" & destinationFolder & "” is not available"
end if
end tell
return newItem as alias
With ASObjC it’s easy to get rid of that.
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
-- Yvan KOENIG (VALLAURIS, France) 19 juin 2020 12:20:33
set origItem to (path to desktop as string) & "pour Musée" -- Exists on my machine, edit to point to an existing item on your machine
set destFolder to (path to documents folder as string) & "unavailableFolder" -- Deliberately missing folder
if not (my itemExistsAtPath:origItem) then error "“" & origItem & "” is unavailable" number -1728
my createFolder:destFolder -- If destFolder is unavailable, try to create it. May return an error
-- case 1
try
my duplicateFileAt:origItem toFolder:destFolder
on error
log "non fatal error"
-- The target item already exists. I assume that we may continue without doing anything.
end try
-- case 2
-- If the item already exists in destFolder, copy the new one appending a dateTime stamp to its name
my duplicateStampedItemAt:origItem toFolder:destFolder
-- case 3
-- If the item already exists in destFolder, delete the existing item and copy the new one
my duplicateItemAtWithReplacing:origItem toFolder:destFolder
#=====
on itemExistsAtPath:HfsPath
set fileManager to current application's NSFileManager's defaultManager()
set POSIXitem to current application's NSString's stringWithString:(POSIX path of HfsPath)
return fileManager's fileExistsAtPath:POSIXitem
end itemExistsAtPath:
#=====
on createFolder:destFolder -- here, destFolder is an Hfs path
set POSIXdest to current application's NSString's stringWithString:(POSIX path of destFolder)
set fileManager to current application's NSFileManager's defaultManager()
if not (fileManager's fileExistsAtPath:POSIXdest) then
-- the destination folder doesn't exist, create it
set destURL to current application's NSURL's fileURLWithPath:POSIXdest
set {theResult, theError} to fileManager's createDirectoryAtURL:destURL withIntermediateDirectories:true attributes:(missing value) |error|:(reference)
if (theResult as boolean) is false then
error (theError's localizedDescription() as string)
end if
end if
end createFolder:
#=====
on duplicateFileAt:sourcePath toFolder:destFolder -- here, sourcePath is an alias and destFolder is an Hfs path
set POSIXsource to current application's NSString's stringWithString:(POSIX path of sourcePath)
set POSIXdest to current application's NSString's stringWithString:(POSIX path of destFolder)
set theNewPath to POSIXdest's stringByAppendingPathComponent:(POSIXsource's lastPathComponent())
set fileManager to current application's NSFileManager's defaultManager()
set {theResult, theError} to fileManager's copyItemAtPath:POSIXsource toPath:theNewPath |error|:(reference)
if (theResult as boolean) is false then
error (theError's localizedDescription() as string)
end if
end duplicateFileAt:toFolder:
#=====
on duplicateItemAtWithReplacing:sourcePath toFolder:destFolder -- here, sourcePath is an alias and destFolder is an Hfs path
set fileManager to current application's NSFileManager's defaultManager()
set POSIXsource to current application's NSString's stringWithString:(POSIX path of sourcePath)
set POSIXdest to current application's NSString's stringWithString:(POSIX path of destFolder)
set theNewPath to POSIXdest's stringByAppendingPathComponent:(POSIXsource's lastPathComponent())
set fileManager to current application's NSFileManager's defaultManager()
if fileManager's fileExistsAtPath:theNewPath then
-- delete the existing item
fileManager's removeItemAtPath:theNewPath |error|:(reference)
end if
set {theResult, theError} to fileManager's copyItemAtPath:POSIXsource toPath:theNewPath |error|:(reference)
if (theResult as boolean) is false then
error (theError's localizedDescription() as string)
end if
end duplicateItemAtWithReplacing:toFolder:
#=====
on duplicateStampedItemAt:sourcePath toFolder:destFolder -- here, sourcePath is an alias and destFolder is an Hfs path
set fileManager to current application's NSFileManager's defaultManager()
set POSIXsource to current application's NSString's stringWithString:(POSIX path of sourcePath)
set POSIXdest to current application's NSString's stringWithString:(POSIX path of destFolder)
set theNewPath to POSIXdest's stringByAppendingPathComponent:(POSIXsource's lastPathComponent())
set fileManager to current application's NSFileManager's defaultManager()
if fileManager's fileExistsAtPath:theNewPath then
set thePathNoExt to theNewPath's stringByDeletingPathExtension()
set stamp to my buildStamp()
set theExtension to POSIXsource's pathExtension()
-- insert the stamp in the file name so it will not duplicate
set theNewPath to (thePathNoExt's stringByAppendingString:stamp)'s stringByAppendingPathExtension:theExtension
end if
set {theResult, theError} to fileManager's copyItemAtPath:POSIXsource toPath:theNewPath |error|:(reference)
if (theResult as boolean) is false then
error (theError's localizedDescription() as string)
end if
end duplicateStampedItemAt:toFolder:
#=====
on buildStamp()
tell (current date) to return "_" & (((its year) * 10000 + (its month) * 100 + (its day)) as string) & "_" & text 2 thru -1 of ((1000000 + (its hours) * 10000 + (its minutes) * 100 + (its seconds)) as string)
end buildStamp
#=====
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 19 juin 2020 11:51:00