Example of ASObjC script to rename a file in a specific directory.
Input: directory path (POSIX path), oldFileName (string) newFileName (string)
return is true or false
If you change the newPath variable and documentDir for it, I believe the handler become a
move file handler.
In this example it use same documentDir so it only rename the file.
use framework "Foundation"
use scripting additions
(**
* Make a file in desktop with name 12345.tiff, it will be renamed to 123.tiff
*)
set thePath to POSIX path of (path to desktop)
my renameFileFromDir:thePath fromName:"12345.tiff" toName:"123.tiff"
on renameFileFromDir:thePath fromName:oldName toName:newName
set documentDir to current application's NSString's stringWithString:thePath
set oldPath to documentDir's stringByAppendingPathComponent:oldName
set newPath to documentDir's stringByAppendingPathComponent:newName
set theSource to current application's |NSURL|'s fileURLWithPath:oldPath
set theDestination to current application's |NSURL|'s fileURLWithPath:newPath
set filemanager to current application's NSFileManager's defaultManager
set {theResult, theError} to filemanager's moveItemAtURL:theSource toURL:theDestination |error|:(reference)
return theResult as text
end renameFileFromDir:fromName:toName:
Ex.
Here we combine rename / move to 1 handler…
Input: directory path input (POSIX path), oldFileName (string), directory path output (POSIX path), newFileName (string)
It will move a file from desktop to home folder… in this example we do not rename the file.
use framework "Foundation"
use scripting additions
set inputPath to POSIX path of (path to desktop)
set targetPath to POSIX path of (path to home folder)
my renameFileFromDirToDestination:inputPath fromName:"123.tiff" toDir:targetPath toName:"123.tiff"
on renameFileFromDirToDestination:inputPath fromName:oldName toDir:outputPath toName:newName
set inputString to current application's NSString's stringWithString:inputPath
set ouputString to current application's NSString's stringWithString:outputPath
set inputPath to inputString's stringByAppendingPathComponent:oldName
set outputPath to ouputString's stringByAppendingPathComponent:newName
set theSource to current application's |NSURL|'s fileURLWithPath:inputPath
set theDestination to current application's |NSURL|'s fileURLWithPath:outputPath
set filemanager to current application's NSFileManager's defaultManager
set {theResult, theError} to filemanager's moveItemAtURL:theSource toURL:theDestination |error|:(reference)
return theResult as text
end renameFileFromDirToDestination:fromName:toDir:toName:
OR
Why not rename multiply files from a list
The great thing about it… handlers could do many things from almost the same idea.
PS. The handler do not check so the count of items of myInputFileList is same as myOutputFileList.
but I guess it would be simple to set a if statement before anything begins.
use framework "Foundation"
use scripting additions
set myInputFileList to {"a.pdf", "b.pdf"}
set myOutputFileList to {"c.pdf", "d.pdf"}
set targetPath to POSIX path of (path to desktop)
repeat with i from 1 to (count myInputFileList)
(my renameFileFromDirToDestination:targetPath fromName:((item i of myInputFileList) as text) toDir:targetPath toName:((item i of myOutputFileList) as text))
end repeat
on renameFileFromDirToDestination:inputPath fromName:oldName toDir:outputPath toName:newName
set inputString to current application's NSString's stringWithString:inputPath
set outputString to current application's NSString's stringWithString:outputPath
set inputPath to inputString's stringByAppendingPathComponent:oldName
set outputPath to outputString's stringByAppendingPathComponent:newName
set theSource to current application's |NSURL|'s fileURLWithPath:inputPath
set theDestination to current application's |NSURL|'s fileURLWithPath:outputPath
set filemanager to current application's NSFileManager's defaultManager
set {theResult, theError} to filemanager's moveItemAtURL:theSource toURL:theDestination |error|:(reference)
return theResult as text
end renameFileFromDirToDestination:fromName:toDir:toName: