Here are two faster versions.
version 1 use standard AppleScript
set tempFolder to (path to temporary items from user domain)
tell application "Finder"
set theSelection to selection
repeat with theAlias in theSelection
if class of theAlias is alias file then
try
set aliasName to name of theAlias
set aliasFolder to container of theAlias
set originalFile to original item of theAlias
set originalFolder to container of originalFile
move theAlias to tempFolder
move originalFile to aliasFolder
move file aliasName of tempFolder to originalFolder
end try
end if
end repeat
end tell
version 2 require more typing (but just have to click a button here to get it).
Using ASObjC it’s faster than the first one.
use scripting additions
use framework "Foundation"
use script "BridgePlus"
load framework
set tempFolder to POSIX path of (path to temporary items from user domain)
tell application "Finder"
set theSelection to selection
repeat with theAlias in theSelection
if class of theAlias is alias file then
try
set originalFile to (get original item of theAlias)
my swap(theAlias as alias, originalFile as alias, tempFolder)
end try
end if
end repeat
end tell
#=====#=====#=====#=====#=====#=====
on swap(anAlias, itsOriginal, tempFolder)
set {aliasName, aliasContainer} to my getNameAndContainer:(POSIX path of anAlias)
set tempPath to my buildFullPath:aliasName inFolder:tempFolder
# moves the alias in the temporay folder
my movePath:(POSIX path of anAlias) toFolder:tempFolder
set {originalName, originalContainer} to my getNameAndContainer:(POSIX path of itsOriginal)
# moves the original in the original alias folder
my movePath:(POSIX path of itsOriginal) toFolder:aliasContainer
# moves the alias in the original original folder
my movePath:tempPath toFolder:originalContainer
end swap
#=====#=====#=====#=====#=====#=====
on getNameAndContainer:POSIXPath # appelé par une instruction
local theURL, PosixContainer, PosixName
set theURL to current application's |NSURL|'s fileURLWithPath:POSIXPath
set PosixContainer to theURL's URLByDeletingLastPathComponent()
set PosixContainer to PosixContainer's |path|() as text
set PosixName to (theURL's lastPathComponent()) as text
# ATTENTION, Sierra ne met pas le / final
return {PosixName, PosixContainer}
end getNameAndContainer:
#=====#=====#=====#=====#=====#=====
on buildFullPath:proposedName inFolder:POSIXPath # appelé par une instruction
local theFolderURL, proposedName, theDestURL
set theFolderURL to current application's |NSURL|'s fileURLWithPath:POSIXPath
if class of proposedName is text then set proposedName to current application's NSString's stringWithString:proposedName
set proposedName to proposedName's stringByReplacingOccurrencesOfString:"/" withString:":"
set theDestURL to theFolderURL's URLByAppendingPathComponent:proposedName
return theDestURL's |path| as text
end buildFullPath:inFolder:
#=====#=====#=====#=====#=====#=====
on movePath:posixSource toFolder:posixDestination # appelé par trois instructions
local theSourceURL, destURL, shortURL, origName, theFileManager, theResult, theError, destName
set theSourceURL to current application's |NSURL|'s fileURLWithPath:posixSource
set destURL to current application's |NSURL|'s fileURLWithPath:posixDestination
set theFileManager to current application's NSFileManager's |defaultManager|()
set {theResult, theError} to theFileManager's createDirectoryAtURL:destURL withIntermediateDirectories:true attributes:(missing value) |error|:(specifier)
--if not (theResult as boolean) then error (theError's |localizedDescription|() as text)
# maintenant, move cheminPosixDuFichierSource item
set destName to theSourceURL's |lastPathComponent|()
set destURL to destURL's URLByAppendingPathComponent:destName
my moveFromURL:theSourceURL toURL:destURL withReplacing:true
--destURL's |path| as text
end movePath:toFolder:
#=====
-- This handler is called by other handler, and is not meant to called directly
on moveFromURL:sourceURL toURL:destinationURL withReplacing:replaceFlag
set theFileManager to current application's NSFileManager's |defaultManager|()
set {theResult, theError} to (theFileManager's moveItemAtURL:sourceURL toURL:destinationURL |error|:(specifier))
if not theResult as boolean then
if replaceFlag and (theError's code() = current application's NSFileWriteFileExistsError) then -- it already exists, so try replacing
-- replace existing file with temp file atomically, then delete temp directory
set {theResult, theError} to theFileManager's replaceItemAtURL:destinationURL withItemAtURL:sourceURL backupItemName:(missing value) options:(current application's NSFileManagerItemReplacementUsingNewMetadataOnly) resultingItemURL:(missing value) |error|:(specifier)
-- if replacement failed, return error
if not theResult as boolean then error (theError's |localizedDescription|() as text)
else -- replaceFlag is false or an error other than file already exists, so return error
error (theError's |localizedDescription|() as text)
end if
end if
end moveFromURL:toURL:withReplacing:
#=====#=====#=====#=====#=====#=====
Most of the code in version 2 is borrowed from Shane Stanley.
Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) dimanche 13 novembre 2016 18:20:00