Script to copy source file and rename from exising text file
Ive been trying to modify existing scripts to duplicate an AI file and rename from existing .txt file -
cant figure it out and need advice/help -
thanks
MacBook Pro 15″, macOS 10.14
Apple Event: May 7th at 7 am PT
Ive been trying to modify existing scripts to duplicate an AI file and rename from existing .txt file -
cant figure it out and need advice/help -
thanks
MacBook Pro 15″, macOS 10.14
> I ran it and the process is sweet except the files arent saving as AI and open in Preview?
The script simply renames based on what's in the text file. I guess I assumed that the text file would include the '.ai' extension, but clearly that was an oversight :)
The simplest fix would be to change the line:
set name of newFile to each_filename
to:
set name of newFile to (each_filename & "_.ai") as text
Your code is close, but fileExt isn't defined in the script and therefore likely to throw an error as an unknown variable.
> I ran it and the process is sweet except the files arent saving as AI and open in Preview?
The script simply renames based on what's in the text file. I guess I assumed that the text file would include the '.ai' extension, but clearly that was an oversight :)
The simplest fix would be to change the line:
set name of newFile to each_filename
to:
set name of newFile to (each_filename & "_.ai") as text
Your code is close, but fileExt isn't defined in the script and therefore likely to throw an error as an unknown variable.
Without seeing the code you're working with, it's impossible to tell what might be wrong.
If you're asking us to write a script for you then you're going to need to provide more information, especially with regards to where your data is coming from - how does the script know where to find the files to duplicate? where should the files be duplicated to? where is the text file of name/renames? what format is that in? etc. etc.
Hi Camelot - thanks for your reply! - i have a script that pulls a list of names from an existing text file and duplicates them based off of a source adobe illustrator file. - But the filenaming structure has changed and the script doesnt perform as needed now and i am manually copying hundreds of filenames from a provided xcel doc and pasting them one by on on a manually duplicated ai file. below is the current script i used before the naming setups changed and i cant modify it to duplicate correctly.
I sincerely appreciate your info if you have time for that! THANKS!
--this version uses the FINDER for copying, and works in both Panther & Tiger...
--set "mainServerPath" to the location of the folder of crossreference data files:
set mainServerPath to "desktop:Reference_files:"
set flexRefFile to "flex_crossref.txt"
set leagueList to {"CFL", "COLLEGE", "HEISMAN", "NBA", "NBADL", "NFL", "NHL"}
set teamNameList to {}
set teamCodeList to {}
-------------------------------------begin user input
set sourceFile to choose file with prompt "Select the SOURCE FILE:" without invisibles
set sourcePath to quoted form of (POSIX path of sourceFile) --The [quoted form of POSIX path] used for UNIX support...
--choose LEAGUE
set selectedLeague to item 1 of (choose from list leagueList with prompt "Select League for this Tech Pack:") as Unicode text
--enter STYLE NUMBER
set styleNum to text returned of (display dialog "Enter Style Number -" default answer "")
--enter COLLECTION PREFIX
set collectionPrefix to text returned of (display dialog "Enter Collection Prefix -" default answer "")
--enter COLOR CODE
set colorCode to text returned of (display dialog "Enter Color Code -" default answer "")
--choose DESTINATION
set destFold to (choose folder with prompt "Select DESTINATION folder:") as Unicode text
set destPath to quoted form of (POSIX path of destFold)
-------------------------------------end of user input
--determine which cross-reference file to use...
if selectedLeague = "CFL" then
set leagueRefPath to mainServerPath & cflRefFile
else if selectedLeague = "COLLEGE" then
set leagueRefPath to mainServerPath & collegeRefFile
else if selectedLeague = "HEISMAN" then
set leagueRefPath to mainServerPath & heismanRefFile
else if selectedLeague = "NBA" then
set leagueRefPath to mainServerPath & nbaRefFile
else if selectedLeague = "NBADL" then
set leagueRefPath to mainServerPath & nbadlRefFile
else if selectedLeague = "NFL" then
set leagueRefPath to mainServerPath & nflRefFile
else if selectedLeague = "NHL" then
set leagueRefPath to mainServerPath & nhlRefFile
end if
--read team code/team name cross reference from a hardcoded location.
set lookupList to read file leagueRefPath -- reads in all contents of the data file
set lookupList to every paragraph of lookupList
-- strip off any empty item at end
if item -1 of lookupList = "" then
set lookupList to items 1 thru -2 of lookupList
end if
--and split the team names and codes into 2 parallel lists...
set AppleScript's text item delimiters to {tab}
repeat with t from 1 to count of lookupList
set end of teamNameList to text item 1 of item t of lookupList
set end of teamCodeList to text item 2 of item t of lookupList
end repeat
set AppleScript's text item delimiters to {""}
--get the file extension of the source file
set fileExt to name extension of (info for sourceFile)
--file copy/renaming occurs now...
repeat with i from 1 to count teamCodeList
--build file name...
set thisTeam to item i of teamNameList
if (count of thisTeam) is less than 5 then
set shortTeam to thisTeam
else
set shortTeam to text 1 thru 5 of thisTeam
end if
set newName to (styleNum & "_" & item i of teamCodeList & "_" & collectionPrefix & shortTeam & "_" & colorCode & "." & fileExt) as Unicode text
--using the FINDER
-- duplicates the source file into the destination, then renames it...
tell application "Finder"
set f to duplicate sourceFile to destFold
set name of f to newName
end tell
end repeat
OK, that helps, but still missing some detail.
> But the filenaming structure has changed and the script doesnt perform as needed
Great, but what changed?
I assume you mean that the target name format (that you want to rename to) has changed. Based on this script, that's all handled by the line:
set newName to (styleNum & "_" & item i of teamCodeList & "_" & collectionPrefix & shortTeam & "_" & colorCode & "." & fileExt) as Unicode text
where it constructs a new name by combining various variables setup earlier in the script (e.g. styleNum, teamCodeList, etc.).
If you want to rename the files to a different format then just amend this line to the format you want, using the concatenation symbol & to link each text component or variable.
If that's not enough you need to provide more detail on what you need to do.
a lot has changed :) - the exisiting script used data that never changed except for the user input. - i no longer need user input and wanted to update to enable me to update the text file (copied/pasted from a provided xcel sheet) as needed, with the results being a duplicated/named AI file that would be selected as the source file to duplicate & rename.
would new code become:
set newName to ("_" & "." & fileExt) as Unicode text
I'm still not quite following.
The script:
set newName to ("_" & "." & fileExt) as Unicode text
would always end up with a file called "_.ai" Is that what you want?
Besides that:
> i no longer need user input
Just nix all the lines that include the display dialog commands.
> wanted to update to enable me to update the text file
You want the script to update the text file used to process the renaming? that's certainly different :)
Is that your goal here? to have the script write the source/destination file names? or actually process the renaming?
yes final file will always be an Adobe Illustrator .ai file - after the script dupes/renames them i would open each file in AI add the needed elements to each named AI file
the filenames are provided ahead of time as an excel file - sometimes its a dozen names, others its a hundred. the names are random like article codes - the goal is this: prior to running the script it would copy/paste the names from the excel file and paste them into an existing TXT file on desktop that I would update as needed - i would like to edit the script, or create a new one that i would first Select Master AI File to copy and rename - once chosen, it duplicates that file and would pull the names for each file from the TXT file -
its similar to what i have but i dont need all the options - just choose a file, choose the tx and rename/duplicate etc... ultimate goal is to eliminate the need to manually copy paste one by one -
thanks for assisting Camelot!
I see, so you have one source file that you want to duplicate numerous times? Makes sense.
How much of this do you want to automate? How much is fixed vs. variable.
For example:
How should the script extract the data from Excel? Is it always a specific column of data? or does it vary? Are you wanting this fully automated? or could you highlight the fields in advance and just have the script look at the selected cells?
Do you even need the intermediate text file if the script can just extract the target filenames from Excel?
How does the script identify the master AI file? Is it always the same or do you want to prompt the user for the file to use?
Where should the duplicates be placed? In a specific folder? Or should the user specify a folder at run time?
Once those points are clear I'm pretty sure we can come up with a solution.
Yes - exactly - the variable would be the list of filenames per each project. The filenames are provided in an unorganized excel file and my thought would be i could select/copy the needed project filenames, paste them into a common text file named "flex.txt" that would live on desktop which i would update per project. This file and the folder the duplicates get saved into could be fixed. Folder name would be "Flex project" - ? - The AI file would also be fixed and thought the user would first choose that file upon running of script - Script reads the filenames from .tx file, duplicates and renames source and places them into the Flex Project folder - ? -
doable?
doable? sure. Still some missing details, though.
Specifically:
"... i could select/copy the needed project filenames, paste them into a common text file named "flex.txt" that would live on desktop which i would update per project."
This doesn't directly answer the question of what the script does at this point (if anything).
Do you want to manually create the text file by copying the various cells' data to the file?
Or do you want the script to extract the data from Excel? either by reference (e.g. 'all cells in Column "A"') or by identifying the selected cells?
Since you say "... I would update per project" that implies you intend to create/maintain this file. That's fine, but if you're going to automate it, why not go all-in? :)
For now I'll assume you want to maintain the file and that the script will just act on whatever data is in the "flex.txt" file.
That said:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "Finder"
-- set the file to read from
set theFlexFile to file "flex.txt" of desktop as alias
-- and read the target file names
set filenames to paragraphs of (read theFlexFile)
-- bail if the file is empty
if (count filenames) = 0 then return
-- set the folder to duplicate to
try
set destFolder to folder "Flex Project" of desktop
on error
-- if we get here the folder doesn't exist, so we need to create it
set destFolder to (make new folder at desktop with properties {name:"Flex Project"})
end try
-- prompt the user for the master file to duplicate
set theSourceFile to choose file with prompt "Select the master AI file:"
-- and duplicate the files
repeat with each_filename in filenames
-- make sure the filename isn't blank
if each_filename as text ≠ "" then
try
-- duplicate the file
set newFile to duplicate theSourceFile to destFolder with replacing
-- and rename it
set name of newFile to each_filename
end try
end if
end repeat
end tell
it's a little more work up-front if you want the script to extract the data from Excel, but the premise is the same.
i agree totally, would love to go all-in but the excel filename cells i receive are unfortunately unorganized and inconsistent, which would be difficult to write a specific script for (i think) - i've tried to explain to others why structure and organization of info would benefit and eventually simplify workflow but changes get perceived as "more work" and people tune out - :\ - but im not giving up :)
The script is almost perfect Camelot! I ran it and the process is sweet except the files arent saving as AI and open in Preview? They save w/out an extension FYI - was thinking it would be .AI at end -
I tried this but didnt work - ?
set name of newFile to each_filename("_" & "." & fileExt) as Unicode text
end try
end if
end repeat
end tell
BINGO BINGO BINGO! THAT DID IT:) THANK YOU CAMELOT!
Thank you for your assistance and expertise!
Script to copy source file and rename from exising text file