Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Simplify a Massive Script with handlers? Need Help Compling

I have the script below that I need to simplify so that when I need to add another source folder I can alter by adding a couple of lines of additional code rather than copy pasting and altering the code.


I'm not sure how to go about it.


The Name of the folder is the Key as this will help identifiy the 'Brand' and also the 'Ratio' (unique to the brand) and the file containing the keyword


property type_list : {"TIFF", "JPEG", "PNGf", "PICT"}

property extension_list : {"tif", "tiff", "jpg", "jpeg", "png", "pict", "psd"}

property weekNumber : missing value

script o -- ***

property csvText : {}

end script

tell application "Finder" to set CompletedFolder to folder (((path to pictures folder) as text) & "CompletedFolders")


try

tell application "Finder" to set bh_folders to (get every folder of CompletedFolder whose name starts with "BH")

if bh_folders is not {} then

set aBHFolder to item 1 of bh_folders

tell application "Finder"

set bh_files to every file of the aBHFolder whose ¬

file type is in the type_list or name extension is in the extension_list

set fName to name of aBHFolder-- to get the basename of the CSV file ***

end tell


if bh_files is not {} then -- ***

set isChanged to false -- ***

set {checkListFile} to my getCSVpath(fName) -- get the CSV path and the week number

set existsCSV to checkListFile is not ""

if existsCSV then set o'scsvText to paragraphs of (readcheckListFileas «class utf8») -- get the contents of the CSV file ***

launch application "Image Events" -- ***

repeat with i from 1 to the count of bh_files

set bh_path to (item i of bh_files) as string

tell application "Image Events"

set bh_image to open file bh_path

copy the dimensions of bh_image to {xbhwidth, ybhheight}


closebh_image

end tell


set bhRatio to 1348 / 1832


if xbhwidth / ybhheight is greater than bhRatio - 1.0E-3 and xbhwidth / ybhheight is less than bhRatio + 1.0E-3 then

my BH_CorrectRatio(xbhwidth, ybhheight, bh_path)

else

my BH_CheckRatio(xbhwidth, ybhheight, bh_path)

end if



-- *** find this name in CSV text ***

if existsCSV and (my findNameInCsv(bh_path)) then set isChanged to true-- a check mark was added in CSV text ***

end repeat

if existsCSV then

if isChanged then -- CSV file must be updated ***

set r to my write_to_file(checkListFile, o'scsvText) -- *** update CSV file ***


-- *** if r is true, the checkList is complete so change the folder label to green ***

if r then tell application "Finder" to set label index of aBHFolder to 6 -- ***

end if

set o'scsvText to {} -- empty this property to not save his contents when the script quit ***

end if

end if

end if

on error error_message


display dialogerror_messagebuttons {"OK"} default button 1


--UPPERCASE CHANGER


set the source_folder to aBHFolder


tell application "Finder"

repeat with this_item in entire contents of source_folder

set the current_name to the name of this_item as text

set the name of this_item to my change_case(the current_name, "upper")


end repeat

end tell



--UPPERCASE CHANGER

end try

on BH_CorrectRatio(xbhwidth, ybhheight, bh_path)


tell application "Finder"

set label index of document file bh_path to 6

end tell

end BH_CorrectRatio




on BH_CheckRatio(xbhwidth, ybhheight, bh_path)


tell application "Finder"

set label index of document file bh_path to 5


end tell

end BH_CheckRatio


---------------Then the next brand and so on......



------then some handlers


on getCSVpath(fName) -- return the path of the CSV file, and the week number

set {tid, text item delimiters} to {text item delimiters, {"_"}}

set tName to text 1 thru text item -2 of fName-- remove the last underscore and characters after it

set text item delimiters to tid

try

set p to "" -- if not exists then the path will be empty

set p to ((path to public folder as string) & "Dropbox:" & "keywords:" & tName & ".csv") as alias

end try

return {p, text -2 thru -1 of tName}

end getCSVpath


on findNameInCsv(f) -- search the exact name from the beginning of each line ***

set {tid, text item delimiters} to {text item delimiters, {":"}}

set tName to last text item of f -- get the filename

set text item delimiters to "."

set thisExt to last text item of tName-- get name extension

set text item delimiters to tid




if thisExt is in extension_list then

set tc to (count thisExt) + 2

set last2chars to text -(tc + 1) thru -tc of tName-- get last 2 characters before name extension--> _02

else -- no extension

set last2chars to text -2 thru -1 of tName-- get last 2 characters --> _02

end if


--*** true if "_02" .... "_12" ***--

tell last2chars to set b to it starts with "_" and (text 2 is in "23456789")

try

if b then set tSku to text 1 thru 16 of tName-- the begining 16 digits, I presume the first 16 characters in the nameon error

set b to false


end try



(*if thisExt is in extension_list then

set tc to (count thisExt) + 2

set last3chars to text -(tc + 2) thru -tc of tName -- get last 3 characters before name extension--> _02

else -- no extension

set last3chars to text -3 thru -1 of tName -- get last 3 characters --> _02

end if

--*** true if "_02" .... "_12" ***--

tell last3chars to set b to it starts with "_" and (text 2 is "0" and text 3 is in "23456789" or text 2 thru 3 is in {"10", "11", "12"})

try

if b then set tSku to text 1 thru 16 of tName -- the begining 16 digits, I presume the first 16 characters in the nameon error

set b to false


end try*)




set n to tName & ","

set tc to counto'scsvText

repeat with i from 1 to tc

set t to item i of o's csvText

if not b and t starts with n or b and (t starts with tSku or t starts with "," & tSku) then -- found

set x to ""

if not b then set item i of o's csvText to "," & t

set text item delimiters to {","}

try

set x2 to text item 2 of t --get original Name

set x3 to (text item 3 of t) & "ALTERNATIVE CODE :_" --get alternative Sku

set x4 to text item 4 of t -- get the keyword

try

set x5 to text item 6 of t--get AIR OR SEA RECORD

on error

set x5 to ""

end try

end try

set text item delimiters to tid


-- exiftool add the keywords to EXIF


if x4 is not "" then do shell script "/usr/bin/exiftool -P -overwrite_original_in_place -keywords+=" & (quoted form of x4) & " -headline=" & (quoted form of x2) & " -source=" & (quoted form of x5) & " " & quoted form of POSIX path of f


return (not b)

end if

end repeat

return false

end findNameInCsv


on write_to_file(the_file, tList) -- update CSV file ***

set n to 0

set n1 to 0

set tc to counto'scsvText

repeat with i from 1 to tc--- ** move lines with check mark to the bottom **

set L to item i of o's csvText

if L is not "" then -- not a blank lines

set n1 to n1 + 1 -- count this valid line

if "" is in L then

set n to n + 1 -- count this check mark

set end of o's csvText to L

set item i of o's csvText to missing value

end if

else

set itemi of o'scsvText to missing value-- remove this blank lines

end if

end repeat

set {tid, text item delimiters} to {text item delimiters, {return}}

set the_data to (text of o'scsvText) as text-- convert list of lines to text

set text item delimiters to tid

try

set openfile to open for accessthe_file with write permission

set eof of openfile to 0


writethe_datatoopenfilestarting at 0 as «class utf8»


close accessopenfile

on error

try


close accessthe_file

end try

end try

return n1 = n-- if the number of lines equal the numbers of check marks

end write_to_file


-- AppleScript 'change_case' handler


on change_case(this_text, this_case)

if this_case is "lower" then

set the comparison_string to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

set the source_string to "abcdefghijklmnopqrstuvwxyz"

else

set the comparison_string to "abcdefghijklmnopqrstuvwxyz"

set the source_string to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

end if

set the new_text to ""

repeat with thisChar in this_text

set x to the offsetofthisCharin the comparison_string

if x is not 0 then

set the new_text to (the new_text & character x of the source_string) as string

else

set the new_text to (the new_text & thisChar) as string

end if

end repeat

return the new_text

end change_case


--UPPERCASE CHANGER

Mac Pro, OS X Mavericks (10.9)

Posted on Nov 28, 2013 6:10 AM

Reply

There are no replies.

Simplify a Massive Script with handlers? Need Help Compling

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.