Sunday, December 17, 2017

#1 2017-11-29 05:05:41 pm

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

Line breaks when writing to file?

How do I tweak this section of code to add line breaks for each item of the list? Currently it just throws it all together. My previous attempts haven't been working, so any help 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
       tell application "Finder" to set writeTarget to ((make new file at folder projectFolder with properties {name:fileSuffix & "_BEFORE"}) as alias) --create BEFORE text file in project folder
       set _BEFORE to all_files
       write (_BEFORE as text) to writeTarget --add "& return" for line breaks?
end tell

Offline

 

#2 2017-11-30 05:10:15 am

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

Re: Line breaks when writing to file?

Applescript:


-- Finder not needed.
set all_files to (choose file with prompt "SELECT FILES:" with multiple selections allowed)

-- Coerce the list of aliases to text using a linefeed as a delimiter.
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. (I'm guessing that projectFolder is an alias or HFS path string.)
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 -- Close the access channel.


NG

Offline

 

#3 2017-11-30 10:39:32 pm

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

Re: Line breaks when writing to file?

Thanks for the tip! However I'm still getting the text items delivered without line breaks (e.g., ;"Macintosh HD:Users:dam:Desktop:Design:Dom4K_Mummy_Ocard_2072017_MECH:HIRES:BORNUHD001_IL01.eps.Macintosh HD:Users:dam:Desktop:Design:Dom4K_Mummy_Ocard_2072017_MECH:HIRES:BORNUHD001_IL02.eps."). I'm wondering if there's a conflict in text delimiters with the start of my code.

I apologize for the code dump. but I'd rather not confuse you any further.

Applescript:

--SETUP
--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
   zipFonts(projectFolder, projCode, seqCount)
else if w is "Rename Zipped" then
   --check for PDF --change this to check for rename zipped font folders
   renameZip(projectFolder, projCode, seqCount)
else if w is "Skip" then
   --check for PDF --change this to check for rename zipped font folders
   checkPDF(projCode, projectFolder, seqCount)
   --else if w is "Cancel" then
   --    display notification "PROCESS CANCELLED"
   --    cancel
end if

--ZIP FONT FOLDER
on zipFonts(projectFolder, projCode, seqCount)
   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
       --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
   --insert _typefaces preview file and project folder prepend
   --set typePrev to ((path to Desktop Folder)
   
   --go to next step
   checkPDF(projCode, projectFolder, seqCount)
end zipFonts

--RENAME ZIPPED FONT FOLDER
on renameZip(projectFolder, projCode, seqCount)
   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
   end tell
   --insert _typefaces preview file and project folder prepend
   --set typePrev to ((path to Desktop Folder)
   
   --go to next step
   checkPDF(projCode, projectFolder, seqCount)
end renameZip

on checkPDF(projCode, projectFolder, seqCount)
   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)
   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)
end checkPDF

on prepPDF(projCode, projectFolder, seqCount)
   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)
end prepPDF

on startPrep(projCode, projectFolder, seqCount)
   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"}
       --set typePreview to this "_TYPEFACES.jpg"
       
       --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
on initSeq(projCode, projectFolder, seqCount)
   --tell application "System Events" to set frontmost of process "SuperPrepScrip_v1_75" to true --comment out if this doesn't keep the dialog box up front; may not work in script test format
   --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"
   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"
   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"
   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"
   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 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
       tell application "Finder" to set writeTarget to ((make new file at folder projectFolder with properties {name:fileSuffix & "_AFTER"}) as alias) --create AFTER text file in project folder
       set _AFTER to all_files
       write (_AFTER as text) to writeTarget
       
       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

 

#4 2017-12-01 05:51:58 am

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

Re: Line breaks when writing to file?

Looking at just the seqFILES() handler, where you've partially implemented my suggestion, linefeeds are inserted into the text which goes into the _BEFORE file. But you haven't made the same changes in the code which writes to the _AFTER file, so you're getting "." between the new file names. (You've set the text item delimiters to "." at the top of the script, so any lists coerced to text will have "." between each item in the result. My code temporarily resets the value to a linefeed, does the coercion, and then resets the delimiters to the state in which it found them.)

Last edited by Nigel Garvey (2017-12-01 06:07:38 am)


NG

Offline

 

#5 2017-12-11 06:18:57 pm

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

Re: Line breaks when writing to file?

Sorry for the delay in getting back to you, it's been a hectic (almost) two weeks. I corrected my error thanks to your help, and it works perfectly now. Thanks a million!

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)