Wednesday, October 17, 2018

#1 2018-09-21 09:30:41 pm

DarthAztek
Member
Registered: 2016-12-07
Posts: 26

Combine separate before/after lists into a single arrayed list?

Hey folks,

Part of my current script deals with renaming files in a folder, and spits out two text files, one before and another after the rename. I'm wondering how would I combine those lists into a single text file where the before and after match (e.g., aOld => aNew, bOld => bNew). Any insight would be appreciated. Thanks!

Applescript:


tell application "Finder"
       --pick files
       set all_files to every item of (choose file with prompt "SELECT " & fileSuffix & " FILES:" with multiple selections allowed) as list
       
       --write before list to root project folder
       set astid to AppleScript's text item delimiters
       set AppleScript's text item delimiters to linefeed
       set _BEFORE to all_files as text
       set AppleScript's text item delimiters to astid
       
       --Open the file for write access, creating it if it doesn't exist.
       set fileAccess to (open for access file ((projectFolder as text) & fileSuffix & "_BEFORE") with write permission)
       try -- Ensure that any errors don't stop the script before it's closed the access channel.
           set eof fileAccess to 0 -- Empty the file as a formality.
           write _BEFORE as «class utf8» to fileAccess -- Write the text to the file as UTF-8.
       end try
       close access fileAccess
       
       --start looping through all selected files. 'index' is our counter that we initially set to 1 and then count up with every file.
       --the 'index' number is required for the sequential renaming of files
       repeat with index from 1 to the count of all_files
           --using our index, we select the appropriate file from our list
           set this_file to item index of all_files
           set file_name_count to text items of (get name of this_file)
           
           --if the index number is lower than 10, we will add a preceding "0" for a proper filename sorting later
           if index is less than 10 then
               set index_prefix to "0"
           else
               set index_prefix to ""
           end if
           
           --check if the current file from our list (based on index-number) has any file-extension
           if number of file_name_count is 1 then
               
               --file_name-count = 1 means, we extracted only 1 text-string from the full file name. So there is no file-extension present.
               set file_extension to ""
           else
               --re-add the original file-extension after changing the name of the file
               set file_extension to "." & item -1 of file_name_count
           end if
           
           --rename file, add the sequential number from 'index' and add the file-extension to it
           set the name of this_file to projCode & fileSuffix & index_prefix & index & file_extension as string
       end repeat
       
       --write after list to desktop
       set astid to AppleScript's text item delimiters
       set AppleScript's text item delimiters to linefeed
       set _AFTER to all_files as text
       set AppleScript's text item delimiters to astid
       
       --Open the file for write access, creating it if it doesn't exist.
       set fileAccess to (open for access file ((projectFolder as text) & fileSuffix & "_AFTER") with write permission)
       try -- Ensure that any errors don't stop the script before it's closed the access channel.
           set eof fileAccess to 0 -- Empty the file as a formality.
           write _AFTER as «class utf8» to fileAccess -- Write the text to the file as UTF-8.
       end try
       close access fileAccess
       
       display notification "File Rename Complete " & index & " files with '" & projCode & "_" & fileSuffix & "' for you."
   end tell

Offline

 

#2 2018-09-22 06:35:27 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4690

Re: Combine separate before/after lists into a single arrayed list?

Hi.

it's probably a good idea to get the paths with the new names while the renaming's taking place, in case the new names cause any reordering of the items in the Finder. You could use text and lists throughout and only use the Finder to rename the files:

Applescript:


--pick files
set all_files to (choose file with prompt "SELECT " & fileSuffix & " FILES:" with multiple selections allowed)

--get _BEFORE_AND_AFTER list, initially just the _BEFORE items
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to linefeed
set _BEFORE_AND_AFTER to paragraphs of (all_files as text) -- List of the original paths.
-- TIDs not reset yet as they're changed a lot during the repeat.

--start looping through all selected files. 'index' is our counter that we initially set to 1 and then count up with every file.
--the 'index' number is required for the sequential renaming of files
repeat with |index| from 1 to (count _BEFORE_AND_AFTER)
   --using our index, we select the appropriate path from our list
   set old_path to item |index| of _BEFORE_AND_AFTER
   
   -- Get both the path to the file's container and the name of the file, omitting any trailing ":" if the "file"'s a bundle.
   if (old_path ends with ":") then set old_path to text 1 thru -2 of old_path
   set AppleScript's text item delimiters to ":"
   set container_path to text 1 thru text item -2 of old_path
   set old_name to text item -1 of old_path
   
   --if the index number is lower than 10, we will add a preceding "0" for a proper filename sorting later
   set file_index to text -2 thru -1 of ((100 + |index|) as text)
   
   --check if the current file from our list (based on index-number) has any file-extension
   if (old_name contains ".") then
       set AppleScript's text item delimiters to "."
       set file_extension to "." & text item -1 of old_name
   else
       set file_extension to ""
   end if
   
   --rename file, add the sequential number from 'index' and add the file-extension to it
   set new_name to projCode & fileSuffix & file_index & file_extension
   tell application "Finder" to set the name of item old_path to new_name
   
   --update the entry in the _BEFORE_AND_AFTER list
   set new_path to container_path & ":" & new_name
   set item |index| of _BEFORE_AND_AFTER to old_path & " => " & new_path
end repeat

--coerce the _BEFORE_AND_AFTER list to text and reset the delimiters
set AppleScript's text item delimiters to linefeed
set _BEFORE_AND_AFTER to _BEFORE_AND_AFTER as text
set AppleScript's text item delimiters to astid

--Open the file for write access, creating it if it doesn't exist.
set fileAccess to (open for access file ((projectFolder as text) & fileSuffix & "_BEFORE_AND_AFTER") with write permission)
try -- Ensure that any errors don't stop the script before it's closed the access channel.
   set eof fileAccess to 0 -- Empty the file as a formality.
   write _BEFORE_AND_AFTER as «class utf8» to fileAccess -- Write the text to the file as UTF-8.
end try
close access fileAccess

display notification "File Rename Complete " & |index| & " files with '" & projCode & "_" & fileSuffix & "' for you."

Edit: Corrected as per ldicroce's comments in the following post.

Last edited by Nigel Garvey (2018-09-23 09:17:39 am)


NG

Offline

 

#3 2018-09-23 07:16:46 am

ldicroce
Member
Registered: 2017-11-25
Posts: 64

Re: Combine separate before/after lists into a single arrayed list?

Hey I found two small typos in one of the last lines:
" write _AFTER as «class utf8» to fileAccess"
it should be:
"write _BEFORE_AND_AFTER as «class utf8» to fileAccess"

"display notification "File Rename Complete " & index & " files with '" & projCode & "_" & fileSuffix & "' for you.""
it should be:
"display notification "File Rename Complete " & (|index| as text) & " files with '" & projCode & "_" & fileSuffix & "' for you.""

And I have a question:
Why do you use |index| instead of something simpler such as "i"?
Is that any particular meaning of the vertical line (pipe)?

Thanks !

Last edited by ldicroce (2018-09-23 07:22:28 am)

Offline

 

#4 2018-09-23 08:15:41 am

Marc Anthony
Member
From:: Dallas, TX
Registered: 2006-04-27
Posts: 832

Re: Combine separate before/after lists into a single arrayed list?

ldicroce wrote:


Why do you use |index| instead of something simpler such as "i"?
Is that any particular meaning of the vertical line (pipe)?



The bookmarked pipe is the mathematical symbol for absolute values; as used here, they enforce the meaning of the variable, which is necessary because "index" is a reserved word. While "i" is simpler, it's also not at all descriptive, which makes it a poor choice for a variable.


Edit: The pipe enforces the literality of the variable itself, like quotation marks for reserved words.

Last edited by Marc Anthony (2018-09-23 06:40:17 pm)

Offline

 

#5 2018-09-23 08:57:31 am

ldicroce
Member
Registered: 2017-11-25
Posts: 64

Re: Combine separate before/after lists into a single arrayed list?

Marc Anthony wrote:
ldicroce wrote:


Why do you use |index| instead of something simpler such as "i"?
Is that any particular meaning of the vertical line (pipe)?

The bookmarked pipe is the mathematical symbol for absolute values; as used here, they enforce the meaning of the variable, which is necessary because "index" is a reserved word. While "i" is simpler, it's also not at all descriptive, which makes it a poor choice for a variable.


I am not sure I have understood.
The line "repeat with x from 1 to 10" implies already that x is an absolute number. So no need to enforce it. Is that correct? I might miss something ...

To have a descriptive name for this variable would the name "my_index" also be equally good? Or the pipe is really acting mathematically on that value?  In other words, is the pipe just a text character or a mathematic operator?

I am saying this since when someone read that script (like myself) might get confused by the pipe symbol ...

Thanks !

Offline

 

#6 2018-09-23 09:34:39 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4690

Re: Combine separate before/after lists into a single arrayed list?

ldicroce wrote:

Hey I found two small typos in one of the last lines:


Thanks! They're now corrected.

"display notification "File Rename Complete " & (|index| as text) & " files with '" & projCode & "_" & fileSuffix & "' for you.""


The as text isn't strictly necessary, because the value of |index| is automatically coerced to text when it's concatenated to the text "File Rename Complete ".

And I have a question:
Why do you use |index| instead of something simpler such as "i"?
Is that any particular meaning of the vertical line (pipe)?


DarthAztek used index in his original post, but as Marc's pointed out, that's a reserved keyword — or at least is on our machines. (I'm not sure what's reserving it, though.) I wanted to use something which looked as similar to the original as possible, so I simply added the vertical bars. index is a reserved keyword; |index| is a legitimate label to use for a variable in AppleScript.


NG

Offline

 

#7 2018-09-23 09:40:52 am

ldicroce
Member
Registered: 2017-11-25
Posts: 64

Re: Combine separate before/after lists into a single arrayed list?

Nigel Garvey wrote:

DarthAztek used index in his original post, but as Marc's pointed out, that's a reserved keyword — or at least is on our machines. (I'm not sure what's reserving it, though.) I wanted to use something which looked as similar to the original as possible, so I simply added the vertical bars. index is a reserved keyword; |index| is a legitimate label to use for a variable in AppleScript.

Got it.
Thanks to both of you !

Offline

 

#8 2018-09-24 01:27:21 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4690

Re: Combine separate before/after lists into a single arrayed list?

I should have added that the purpose of vertical bars in AppleScript is to allow terms and characters which wouldn't normally be allowed to be used as/in labels:

Applescript:

set |index| to 7
set |my favourite variable| to "Hello"
set |x - 1| to x - 1
set |⌘| to current application

But they should really only be used where there are no sensible alternatives.  smile

Last edited by Nigel Garvey (2018-09-24 01:40:24 am)


NG

Offline

 

#9 2018-09-25 09:58:18 pm

DarthAztek
Member
Registered: 2016-12-07
Posts: 26

Re: Combine separate before/after lists into a single arrayed list?

Hey folks, thanks so much for the assist. My first attempt gave me the error that said "new_name" was not defined. Did I miss something? I'd prefer not to 'code bomb' you guys with the entire script, but if needed for context, I'll do so.

Offline

 

#10 2018-09-26 01:53:52 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4690

Re: Combine separate before/after lists into a single arrayed list?

DarthAztek wrote:

My first attempt gave me the error that said "new_name" was not defined. Did I miss something?


Hi DarthAztek.

I can't see why you should be getting that error. In my code above, new_name is set near the bottom of the repeat, is immediately used to rename the file, and is then appended to the container folder path to create an _AFTER path (new_path) to put in the text file. These are the only places it's used.

There are a few variables which aren't defined in the code — ie. fileSuffix, projCode, and projFolder — which aren't defined in your posted script either. I've assumed these are set in code which precedes the snippet you posted. Both projCode and fileSuffix are used in the setting of new_name.

I'm just about to go away for a few days, so if you post again and I don't reply immediately, it won't be because I've lost interest.  smile


NG

Offline

 

#11 2018-09-26 02:00:19 pm

DarthAztek
Member
Registered: 2016-12-07
Posts: 26

Re: Combine separate before/after lists into a single arrayed list?

Thanks and duly noted. Here's the full script—with your additions—to help with the context. I'm hoping won't be a horrible mess to swim through. Don't mind my notes elsewhere in the script, I'm making tweaks elsewhere to it.

Applescript:


--SETUP: version 2.00a
--set delimiters for filename sequence
set text item delimiters to "."
--set default loop count
set seqCount to 1

--get job code, format ABCDEXX###
display dialog "ENTER JOB CODE:" default answer "JobCode"
set projCode to text returned of result

--designate project folder
set projectFolder to choose folder with prompt "SELECT PROJECT FOLDER for " & projCode

--choose zip/nozip here, else jump to startPrep
display dialog "NEED TO ZIP/RENAME ZIPPED FONTS?" buttons {"Zip Fonts", "Rename Zipped", "Skip"} default button 1 with icon caution
set w to button returned of result
if w is "Zip Fonts" then
   --zip fonts folder
   set typePreviewNeed to "yes"
   zipFonts(projectFolder, projCode, seqCount, typePreviewNeed)
else if w is "Rename Zipped" then
   --rename zipped font file
   set typePreviewNeed to "yes"
   renameZip(projectFolder, projCode, seqCount, typePreviewNeed)
else if w is "Skip" then
   --check for PDF
   set typePreviewNeed to "no"
   checkPDF(projCode, projectFolder, seqCount, typePreviewNeed)
end if

--ZIP FONT FOLDER
on zipFonts(projectFolder, projCode, seqCount, typePreviewNeed)
   tell application "Finder"
       --set zip filename append pre suffix
       set zipSuffix to "_TYPEFACES"
       
       --pick font folder
       set inputFolder to choose folder default location projectFolder with prompt "SELECT FONT FOLDER for " & projCode
       
       --path to _Fonts_ReadMe.txt
       set fontsReadMe to (path to me as string) & "Contents:Resources:_Fonts_ReadMe.txt"
       --duplicate file to font folder
       set dupeRM to duplicate of fontsReadMe to inputFolder
       
       --execute shell command to zip
       set qpp to quoted form of POSIX path of inputFolder
       do shell script "cd $(dirname " & qpp & ")
       zip -r \"$(basename "
& projCode & "_TYPEFACES" & ").zip\" \"$(basename " & qpp & ")\""
       --delete the original font folder
       delete folder inputFolder
       
       --notify completion
       display notification "FONTS ZIPPED"
   end tell
   
   --go to next step
   checkPDF(projCode, projectFolder, seqCount, typePreviewNeed)
end zipFonts

--RENAME ZIPPED FONT FOLDER
on renameZip(projectFolder, projCode, seqCount, typePreviewNeed)
   tell application "Finder"
       set mainZIP to (choose file default location projectFolder with prompt "SELECT ZIPPED FONTS for " & projCode)
       --get the filename to save the extension
       set mainZIPCount to text items of (get name of mainZIP)
       --pull original filename extension
       if number of mainZIPCount is 1 then
           set mainExt to ""
       else
           set mainExt to "." & item -1 of mainZIPCount
       end if
       --rename file with extension (Finder removes extensions by default when renaming)
       set the name of mainZIP to projCode & "_TYPEFACES" & mainExt as string
       --copy fontsReadMe to projectFolder
       set fontsReadMe to (path to me as string) & "Contents:Resources:_Fonts_ReadMe.txt"
       set dupeRM to duplicate of fontsReadMe to projectFolder
   end tell
   
   --go to next step
   checkPDF(projCode, projectFolder, seqCount, typePreviewNeed)
end renameZip

on checkPDF(projCode, projectFolder, seqCount, typePreviewNeed)
   display dialog "PROJECT CONTAINS PDF OF MECHANICAL?" buttons {"Yes", "No", "Cancel"} default button 1 with icon caution
   set v to button returned of result
   if v is "Yes" then
       --go to PDF process
       prepPDF(projCode, projectFolder, seqCount, typePreviewNeed)
   else if v is "No" then
       --skip step and start prep
       display notification "SKIPPING STEP PLEASE CREATE PDF MANUALLY"
   else if v is "Cancel" then
       display notification "PROCESS CANCELLED"
       cancel
   end if
   startPrep(projCode, projectFolder, seqCount, typePreviewNeed)
end checkPDF

on prepPDF(projCode, projectFolder, seqCount, typePreviewNeed)
   tell application "Finder"
       --DESIGNATE MAIN PDF
       set mainPDF to (choose file default location projectFolder with prompt "SELECT MAIN PDF FILE for " & projCode)
       --get the filename to save the extension
       set mainFNCount to text items of (get name of mainPDF)
       --pull original filename extension
       if number of mainFNCount is 1 then
           set mainExt to ""
       else
           set mainExt to "." & item -1 of mainFNCount
       end if
       --rename file with extension (Finder removes extensions by default when renaming)
       set the name of mainPDF to projCode & "_PDF01" & mainExt as string
   end tell
   
   --continue to next step
   startPrep(projCode, projectFolder, seqCount, typePreviewNeed)
end prepPDF

on startPrep(projCode, projectFolder, seqCount, typePreviewNeed)
   --try omitting the tells?
   tell application "Finder"
       --CREATE NEW PROJECT FOLDERS AND SORT FILES
       make new folder at projectFolder with properties {name:"HIRES"}
       --set path to move to HIRES folder
       set hiFiles to (projectFolder as string) & "HIRES"
       --designate files to move into HIRES
       set targetFiles to get every item of (entire contents of projectFolder) whose kind ≠ "Folder"
       --move files into HIRES
       move targetFiles to hiFiles
       --remove extraneous folders
       delete (every folder of projectFolder whose name is not "HIRES")
       --create preview folder
       make new folder at projectFolder with properties {name:"PREVIEWS"}
       --copy font typeface preview file to PREVIEW folder
       if typePreviewNeed = "yes" then
           set typePreview to (path to me as string) & "Contents:Resources:_TYPEFACES.jpg"
           set dupeTP to (duplicate of typePreview to folder of folder projectFolder with name) = "PREVIEWS" --rework line of code, not working
           set name of dupeTP to newName
           set newName of dupeRM to projCode & "_TYPEFACES.jpg"
       end if
       
       --DESIGNATE MAIN MECHANICAL
       set mainMech to (choose file default location projectFolder with prompt "SELECT MAIN MECHANICAL FILE for " & projCode)
       --get the filename to save the extension
       set mainFNCount to text items of (get name of mainMech)
       --pull original filename extension
       if number of mainFNCount is 1 then
           set mainExt to ""
       else
           set mainExt to "." & item -1 of mainFNCount
       end if
       --rename file with extension (Finder removes extensions by default when renaming)
       set the name of mainMech to projCode & mainExt as string
       
   end tell
   
   --start next step
   initSeq(projCode, projectFolder, seqCount)
end startPrep

--RENAME/SEQUENCE SUPPORT FILES
--check current initSeq count
--for later: put in code to bring app to the front during the seqArt, seqBit, etc loops?
on initSeq(projCode, projectFolder, seqCount)
   --default sequence order
   if seqCount = 1 then
       seqART(projCode, projectFolder, seqCount)
   else if seqCount = 2 then
       seqBIT(projCode, projectFolder, seqCount)
   else if seqCount = 3 then
       seqIL(projCode, projectFolder, seqCount)
   else if seqCount = 4 then
       seqLYRD(projCode, projectFolder, seqCount)
   else if seqCount > 4 then
       --extract project code for folder prepend
       set prePend to text -5 thru -1 of projCode
       --prepend project code to projectFolder name
       tell application "System Events" to set name of projectFolder to (prePend & " - " & (get name of projectFolder))
       --reset count for next project
       set seqCount to 1
       display notification "PLEASE CREATE PREVIEWS AND RELINK TO MECHANICAL." with title "PROJECT " & projCode & " PREP COMPLETED." sound name "Purr"
       delay 2
       quit
   end if
end initSeq

--sequence through different support files
--sequence _ART
on seqART(projCode, projectFolder, seqCount)
   set fileSuffix to "_ART"
   tell application "System Events" to set frontmost of process "SuperPrepScrip_v2_00" to true --comment out if this doesn't keep the dialog box up front; may not work in script test format
   display dialog "Rename/Sequence _ART?" buttons {"Yes", "Skip", "Cancel"} default button 1 with icon caution
   set x to button returned of result
   if x is "Yes" then
       seqFILES(fileSuffix, projectFolder, projCode, seqCount)
   else if x is "Skip" then
       set seqCount to seqCount + 1
       seqBIT(projCode, projectFolder, seqCount)
   else if x is "Cancel" then
       delay 2
       display notification "PROCESS CANCELLED" sound name "Basso"
       cancel
   end if
end seqART

--sequence _BIT
on seqBIT(projCode, projectFolder, seqCount)
   set fileSuffix to "_BIT"
   tell application "System Events" to set frontmost of process "SuperPrepScrip_v2_00" to true --comment out if this doesn't keep the dialog box up front; may not work in script test format
   display dialog "Rename/Sequence _BIT?" buttons {"Yes", "Skip", "Cancel"} default button 1 with icon caution
   set x to button returned of result
   if x is "Yes" then
       seqFILES(fileSuffix, projectFolder, projCode, seqCount)
   else if x is "Skip" then
       set seqCount to seqCount + 1
       seqIL(projCode, projectFolder, seqCount)
   else if x is "Cancel" then
       delay 2
       display notification "PROCESS CANCELLED" sound name "Basso"
       cancel
   end if
end seqBIT

--sequence _IL
on seqIL(projCode, projectFolder, seqCount)
   set fileSuffix to "_IL"
   tell application "System Events" to set frontmost of process "SuperPrepScrip_v2_00" to true --comment out if this doesn't keep the dialog box up front; may not work in script test format
   display dialog "Rename/Sequence _IL?" buttons {"Yes", "Skip", "Cancel"} default button 1 with icon caution
   set x to button returned of result
   if x is "Yes" then
       seqFILES(fileSuffix, projectFolder, projCode, seqCount)
   else if x is "Skip" then
       set seqCount to seqCount + 1
       seqLYRD(projCode, projectFolder, seqCount)
   else if x is "Cancel" then
       delay 2
       display notification "PROCESS CANCELLED" sound name "Basso"
       cancel
   end if
end seqIL

--sequence _LYRD
on seqLYRD(projCode, projectFolder, seqCount)
   set fileSuffix to "_LYRD"
   tell application "System Events" to set frontmost of process "SuperPrepScrip_v2_00" to true --comment out if this doesn't keep the dialog box up front; may not work in script test format
   display dialog "Rename/Sequence _LYRD?" buttons {"Yes", "Skip", "Cancel"} default button 1 with icon caution
   set x to button returned of result
   if x is "Yes" then
       seqFILES(fileSuffix, projectFolder, projCode, seqCount)
   else if x is "Skip" then
       set seqCount to seqCount + 1
       initSeq(projCode, projectFolder, seqCount)
   else if x is "Cancel" then
       delay 2
       display notification "PROCESS CANCELLED" sound name "Basso"
       cancel
   end if
end seqLYRD

--rename support files in sequence
on seqFILES(fileSuffix, projectFolder, projCode, seqCount)
   --RENAME ALL SUPPORT FILES IN SEQUENCE
   --tell application "Finder"
   --pick files
   set all_files to (choose file with prompt "SELECT " & fileSuffix & " FILES:" with multiple selections allowed)
   
   --get _BEFORE_AND_AFTER list, initially just the _BEFORE items
   set astid to AppleScript's text item delimiters
   set AppleScript's text item delimiters to linefeed
   set _BEFORE_AND_AFTER to paragraphs of (all_files as text) -- List of the original paths.
   -- TIDs not reset yet as they're changed a lot during the repeat.
   
   --start looping through all selected files. 'index' is our counter that we initially set to 1 and then count up with every file.
   --the 'index' number is required for the sequential renaming of files
   repeat with |index| from 1 to (count _BEFORE_AND_AFTER)
       --using our index, we select the appropriate path from our list
       set old_path to item |index| of _BEFORE_AND_AFTER
       
       -- Get both the path to the file's container and the name of the file, omitting any trailing ":" if the "file"'s a bundle.
       if (old_path ends with ":") then set old_path to text 1 thru -2 of old_path
       set AppleScript's text item delimiters to ":"
       set container_path to text 1 thru text item -2 of old_path
       set old_name to text item -1 of old_path
       
       --if the index number is lower than 10, we will add a preceding "0" for a proper filename sorting later
       set file_index to text -2 thru -1 of ((100 + |index|) as text)
       
       --check if the current file from our list (based on index-number) has any file-extension
       if (old_name contains ".") then
           set AppleScript's text item delimiters to "."
           set file_extension to "." & text item -1 of old_name
       else
           set file_extension to ""
       end if
       
       --rename file, add the sequential number from 'index' and add the file-extension to it
       set new_name to projCode & fileSuffix & file_index & file_extension
       tell application "Finder" to set the name of item old_path to new_name
       
       --update the entry in the _BEFORE_AND_AFTER list
       set new_path to container_path & ":" & new_name
       set item |index| of _BEFORE_AND_AFTER to old_path & " => " & new_path
   end repeat
   
   --coerce the _BEFORE_AND_AFTER list to text and reset the delimiters
   set AppleScript's text item delimiters to linefeed
   set _BEFORE_AND_AFTER to _BEFORE_AND_AFTER as text
   set AppleScript's text item delimiters to astid
   
   --Open the file for write access, creating it if it doesn't exist.
   set fileAccess to (open for access file ((projectFolder as text) & fileSuffix & "_BEFORE_AND_AFTER") with write permission)
   try -- Ensure that any errors don't stop the script before it's closed the access channel.
       set eof fileAccess to 0 -- Empty the file as a formality.
       write _BEFORE_AND_AFTER as «class utf8» to fileAccess -- Write the text to the file as UTF-8.
   end try
   close access fileAccess
   
   display notification "File Rename Complete " & |index| & " files with '" & projCode & "_" & fileSuffix & "' for you."
   --end tell
   
   --add in increment to seqCount and restart sequence
   set seqCount to seqCount + 1
   initSeq(projCode, projectFolder, seqCount)
   
end seqFILES

Offline

 

#12 2018-09-30 07:06:16 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4690

Re: Combine separate before/after lists into a single arrayed list?

Hi.

Well. I'm afraid it's the same question mark, even with the full script. There are only two lines in the entire code which try to read the contents of a variable called new_name and they immediately follow a line which sets its value. Unless you've accidentally deleted this line or commented it out, there's no way you can be getting an error saying that "The variable new_name is not defined."  hmm


NG

Offline

 

#13 2018-10-11 09:16:24 pm

DarthAztek
Member
Registered: 2016-12-07
Posts: 26

Re: Combine separate before/after lists into a single arrayed list?

Ohyehgawds, it's not "new_name" that's throwing the error, it's "newName" that happens earlier in the script during the startPrep handler. I feel dumb now, staring at this code for like a week, especially since I left myself a note to fix the code (DUH) after trying to tweak it. No wonder I'm hitting a wall. I've since taken out "newName" to not confuse myself again.

The goal was to put a copy of a jpg into the newly-created "PREVIEWS" folder from the app version of the script (in the package's "Resouces" folder), snippet of the borked code below:

Applescript:

       if typePreviewNeed = "yes" then
           set typePreview to (path to me as string) & "Contents:Resources:_TYPEFACES.jpg"
           set dupeTP to (duplicate of typePreview to folder of folder projectFolder with name) = "PREVIEWS" --rework line of code, not working
           set name of dupeTP to projCode & "_TYPEFACES.jpg"--erroring out, unable to rename the file.
       end if

It's similar to the end of the renameZip handler earlier in the script, snipped below:

Applescript:

--copy fontsReadMe to projectFolder
       set fontsReadMe to (path to me as string) & "Contents:Resources:_Fonts_ReadMe.txt"
       set dupeRM to duplicate of fontsReadMe to projectFolder

Sorry about my error causing the confusion!

Last edited by DarthAztek (2018-10-11 09:16:56 pm)

Offline

 

#14 2018-10-12 03:01:49 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4690

Re: Combine separate before/after lists into a single arrayed list?

Aha! I might have noticed that myself if I'd read the entire script. But I just used Find… in Script Editor to see where new_name appeared in the code.  roll

Apart from newName only being set after it's used, there are a few issues with the 'set dupeTP …' line.

1. duplicate is a command, not a property, so it shouldn't be followed by of.
2. typePreview has been set to text, so it should either be coerced to alias or be preceded by file or item in the duplicate command. Alternatively, you may prefer to use path to resource (another StandardAdditions command), which returns an alias anyway.
3. folder of happens to return the folder in which a Finder item occurs, but for clarity and conformity with the Finder's dictionary, it should be container of (assuming that this is actually what's wanted).
4. The Finder's duplicate command doesn't have a name or with name parameter.
5. The line sets dupeTP to the result of testing whether or not the result of duplicate is the text "PREVIEWS" — that is, the boolean false — so you won''t be able to set its name to anything in the following line.

Applescript:


   if typePreviewNeed = "yes" then
       -- set typePreview to ((path to me as text) & "Contents:Resources:_TYPEFACES.jpg") as alias
       set typePreview to (path to resource "_TYPEFACES.jpg" in bundle (path to me))
       set dupeTP to (duplicate typePreview to container of folder projectFolder)
       set name of dupeTP to projCode & "_TYPEFACES.jpg"
   end if

Last edited by Nigel Garvey (2018-10-12 03:05:18 am)


NG

Offline

 

#15 Yesterday 03:38:22 pm

DarthAztek
Member
Registered: 2016-12-07
Posts: 26

Re: Combine separate before/after lists into a single arrayed list?

Thanks for the assist! That helps immensely. It does correctly drop a copy of the resource file, but it put it into the root folder (the container part of the line), rather into an existing subfolder, which would be projectFolder/PREVIEWS., but I've been unsuccessful in the proper syntax for that.

Offline

 

#16 Today 01:29:51 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4690

Re: Combine separate before/after lists into a single arrayed list?

I think you mean this:

Applescript:

set dupeTP to (duplicate typePreview to folder "PREVIEWS" of folder projectFolder)


NG

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)