I have tokenFile set up as the alias to the file I am processing. It is in a folder named B that is nested in a folder named A. I want to duplicate tokenFile to folder A (leaving the original file intact in folder B), rename the duplicate to append the name of folder B to the beginning of tokenFile’s filename, and have the newly duplicated and renamed file in folder A continue to be used as the tokenFile alias for further processing. The script needs to determine the folders by their containers, not via a hard coded name.
I’ve tried several ways to do it, but I always get some sort of “can’t make” error.
Can anyone lead me down the right path? I’m sure I’m missing something simple…
Browser: Safari 419.3
Operating System: Mac OS X (10.4)
Sorry. I really hit a wall trying to figure it out, and my lack of a good AppleScript foundation really showed through
set tokenFile to (tokenFile) as alias -- gets the path of dropped file
tell application "Finder" -- These need to be set in the finder
set tokenFolder to container of tokenFile as alias -- Gets parent folder path
set tokenFileName to name of tokenFile as text -- establishes filename of dropped file
set tokenFolderName to name of container of tokenFile as text -- establishes filename of parent folder of dropped file
set tokenGrandFolder to container of tokenFolder as alias -- Gets grand-parent folder path
set nameAppendage to name of tokenFolder & "-"
set tokenFile to duplicate tokenFile to tokenGrandFolder
set name of tokenFile to nameAppendage & tokenFileName -- Adds folder name to beginning of filename
end tell
Browser: Safari 523.12.2
Operating System: Mac OS X (10.4)
set tokenFile to tokenFile as alias -- gets the path of dropped file
tell application "Finder" -- These need to be set in the finder
set dupFile to (duplicate tokenFile) as alias
set name of dupFile to (name of container of dupFile & "-" & name of tokenFile)
duplicate dupFile to container of container of tokenFile
end tell
This is one of the problems I dealt with. This code duplicates and renames the original file, but it then grabs the original file and tries to move it, not the new file…it like changing the name doesn’t register to the alias’ed variable, so it stays with the old file as tokenFile.
I want the original file to remain untouched, and have the duped and renamed file be what is moved and set as tokenFile (for further processing by another area of the script).
The only way I’ve achieved this so far is after code similar to yours, is to set a tokenFile to variable with a hard-coded filepath and filename - but I may not always know what that path is, so I’m trying to find a way that’s not hard-coded.
OK, so I think I’ve realied what may be my real problem…nfs
Your script works fine on my local computer, but the files I’m going to be processing with my script are on a server connected via nfs - and it doesn’t seem to like the renaming of the file. It errors to saying a resource wasn’t found, and when I look into it, I discover that when it’s time to do the second duplicate, it continues to look for the original duplicated filename (myfilename copy.pdf) instead of the renamed version (containername-myfilename.pdf). It works fine on the nfs server, if I remove this line:
set name of dupFile to (name of container of dupFile & "-" & name of tokenFile)
Ideally I’d want this to work either locally or on the nfs share…
set tokenFile to tokenFile as alias -- gets the path of dropped file
set {ASTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "/"}
tell POSIX path of tokenFile to set {tokenFileName, tokenFolderName, tokenFolder, tokenGrandFolder} to {text item -1, text item -2, text items 1 thru -2 as text, text items 1 thru -3 as text}
set AppleScript's text item delimiters to ASTID
set newName to tokenFolderName & "-" & tokenFileName
do shell script "cp " & quoted form of POSIX path of tokenFile & space & quoted form of (tokenFolder & "/" & newName)
do shell script "cp " & quoted form of POSIX path of tokenFile & space & quoted form of (tokenGrandFolder & "/" & newName)
set tokenFile to (tokenFolder & "/" & newName) as POSIX file as alias
the cp command seems to be automatically overwriting any existing files with the same name without asking. That’s fine in this case, I’m just making sure that’s what is supposed to happen.