It’s been a while, but I am totally stuck on this script I have been working on. I have it checking a file that is being written to the holding folder, once it has finished being written it then needs to add the text ‘_HR’ to the end of the filename and then move it to another folder.
I have two out of three parts working, but I just can’t work out how to add the ‘_HR’ part. If anyone can point me in the right direction it would be awesome…
property theWatchedFTPFolder : "MacSSD:Users:liam:Desktop:ftpTest:ftpholding:"
property theFTPFolder : "MacSSD:Users:liam:Desktop:ftpTest:remoteFTP:"
--Begin workflow
tell application "Finder" to set theFTPFiles to every file of folder theWatchedFTPFolder as alias list
--Verify file finished copying
to _verifyFile(aFTPFile, fullVerify) -- aFile is alias
repeat
set was to (info for aFTPFile with size)
delay 2
set isNow to (info for aFTPFile as alias with size)
if was = isNow then exit repeat
end repeat
if fullVerify then
tell application "Finder"
end tell
end if
return true
end _verifyFile
--Rename and Move PDF
repeat with aFTPFile in theFTPFiles
if (_verifyFile(aFTPFile, true)) then
tell application "Finder" to move file (aFTPFile) to theFTPFolder with replacing
end if
end repeat
Your problem is likely that you have the filenames as aliases. Each alias requires a conversion to text, then append your letters to the text, convert back to an alias, and rename the file with the new alias.
bigdrunk. I’m not sure I entirely understand your script but I would use the shell’s mv command to both move the file and append “_HR” (see the moveFile handler below).
FWIW, I thought I might suggest a different approach.
set sourceFolder to "/Users/Robert/Working/" -- change to applicable path
set destinationFolder to "/Users/Robert/Downloads/" -- change to applicable path
set copyTimeout to 20
set ATID to AppleScript's text item delimiters
tell application "System Events"
set allFiles to POSIX path of every file in folder sourceFolder whose visible is true
repeat with aFile in allFiles
set aFile to contents of aFile
repeat with i from 1 to copyTimeout
set fileSize to size of file aFile
delay 2
if fileSize = (size of file aFile) then
my moveFile(aFile, destinationFolder)
exit repeat
else if i = copyTimeout then
display dialog "File copy timed out" buttons "OK" cancel button 1 default button 1
end if
end repeat
end repeat
end tell
set AppleScript's text item delimiters to ATID
on moveFile(theFile, destinationFolder)
set AppleScript's text item delimiters to {"/"}
set fileName to text item -1 of theFile
set AppleScript's text item delimiters to {"."}
set theName to text 1 thru text item -2 of fileName
set theExtension to text item -1 of fileName
set destinationFile to destinationFolder & theName & "_HR" & "." & theExtension
do shell script "cp -f " & quoted form of theFile & space & quoted form of destinationFile
end moveFile
BTW, I used the cp command above just for testing. This can be changed to mv if everything works as you want. Also, I wasn’t able to test this fully because I can’t easily duplicate the source folder that is being written to. Finally, I wondered if there might be a better way to determine when writing to a source file is complete but I couldn’t find one.
Thank you, the approach you tried works for me. The only issue is that the _HR is placed at the end of the filename after the .PDF suffix. Other approaches I tried had this issue and I haven’t been able to figure out how to add the _HR to the text before the .PDF
I’ve modified my script in post 3 to fix this. Please note that the script will not work properly if a source file does not contain a file extension.
If you want to continue working with your original script, the following uses the Finder to move and rename the source file.
set theFile to "Macintosh HD:Users:Robert:Working:New Text File.txt" as alias
set destinationFolder to "Macintosh HD:Users:Robert:Downloads:" as alias
tell application "Finder"
set fileName to name of theFile
set theExtension to name extension of theFile
set baseName to text 1 thru -((count theExtension) + 2) of fileName
set newName to baseName & "_HR" & "." & theExtension --> "New Text File_HR.txt"
set theNewFile to move theFile to destinationFolder
set name of theNewFile to newName
end tell
Just out of general interest, I did some research and testing to determine whether the busy-status property of the info-for command or the busy-status property of disk item of System Events might be used in the OP’s script to determine when writing to a file was complete. The simple answer is no–neither one can be reliably used for this purpose. It’s not entirely clear to me why that’s the case, although the AppleScript Language Guide contains the following: