Pastebin PRO Accounts SUMMER SPECIAL! For a limited time only get 40% discount on a LIFETIME PRO account! Offer Ends Soon!
SHARE
TWEET
Keyword CSV
a guest
May 11th, 2016
3
Never
- property type_list : {"TIFF", "JPEG", "PNG", "PICT"}
- property extension_list : {"tif", "tiff", "jpg", "jpeg", "png", "pict", "psd"}
- set fileMatrix to {¬
- {BrandInitials:"BHS", theRatios:{1348 / 1832}}, ¬
- {BrandInitials:"Boots", theRatios:{1196 / 1300}}, ¬
- {BrandInitials:"Burton", theRatios:{1020 / 1385}}, ¬
- {BrandInitials:"Castro", theRatios:{1}}, ¬
- {BrandInitials:"Daisy", theRatios:{1280 / 1740}}, ¬
- {BrandInitials:"Diva", theRatios:{1000 / 1360}}, ¬
- {BrandInitials:"Dorothy", theRatios:{1000 / 1360}}, ¬
- {BrandInitials:"Freedom", theRatios:{2 / 3}}, ¬
- {BrandInitials:"GinaT", theRatios:{19 / 23}}, ¬
- {BrandInitials:"Glue", theRatios:{2 / 3}}, ¬
- {BrandInitials:"KidsBHS", theRatios:{1348 / 1832}}, ¬
- {BrandInitials:"KidsMatalan", theRatios:{1000 / 1400}}, ¬
- {BrandInitials:"Matalan", theRatios:{1000 / 1400}}, ¬
- {BrandInitials:"Mikey", theRatios:{1500 / 2000}}, ¬
- {BrandInitials:"Norstrom", theRatios:{2 / 3}}, ¬
- {BrandInitials:"Tally", theRatios:{806 / 1212}}, ¬
- {BrandInitials:"Topman", theRatios:{1020 / 1385}}, ¬
- {BrandInitials:"V&D", theRatios:{1}}, ¬
- {BrandInitials:"Wallis", theRatios:{2 / 3}}, ¬
- {BrandInitials:"WE", theRatios:{1}}, ¬
- {BrandInitials:"Press", theRatios:{1000 / 1410}}, ¬
- {BrandInitials:"Season", theRatios:{1000 / 1410}}}
- script o -- ***
- property csvText : {}
- end script
- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- try
- tell application "Finder" to set CompletedFolder to folder (((path to home folder) as text) & "Dropbox:CompletedFolders")
- on error
- tell application "Finder" to set CompletedFolder to folder (((path to startup disk) as text) & "Volumes:CloudStorage:Dropbox:CompletedFolders:")
- end try
- repeat with matrixItem in fileMatrix
- my KeyworderChecker((BrandInitials of matrixItem), (theRatios of the matrixItem), CompletedFolder)
- end repeat
- on KeyworderChecker(BrandName, Ratio, CompletedFolder)
- try
- tell application "Finder" to set the_Folders to (get every folder of CompletedFolder whose name starts with BrandName)
- if the_Folders is not {} then
- set a_folder to item 1 of the_Folders
- tell application "Finder"
- set filesOfFolder to every file of the a_folder whose ¬
- file type is in the type_list or name extension is in the extension_list
- set fName to name of a_folder -- to get the basename of the CSV file ***
- end tell
- if BrandName is equal to "KidsBHS" then
- set ThewkNumber to (text 9 thru -5 of fName)
- set fName to "Kids_" & ThewkNumber & "_PSD"
- end if
- if BrandName is equal to "KidsMatalan" then
- set ThewkNumber to (text 13 thru -5 of fName)
- set fName to "Kids_" & ThewkNumber & "_PSD"
- log "fname " & fName
- end if
- if filesOfFolder 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's csvText to paragraphs of (read checkListFile as «class utf8») -- get the contents of the CSV file ***
- launch application "Image Events" -- ***
- repeat with i from 1 to the count of filesOfFolder
- set t_path to (item i of filesOfFolder) as string
- tell application "Image Events"
- set t_image to open file t_path
- copy the dimensions of t_image to {xwidth, yheight}
- close t_image
- end tell
- if xwidth / yheight is greater than Ratio - 1.0E-3 and xwidth / yheight is less than Ratio + 1.0E-3 then
- my CorrectRatio(t_path)
- else
- my CheckRatio(t_path)
- end if
- -- *** find this name in CSV text ***
- if existsCSV and (my findNameInCsv(t_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's csvText) -- *** 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 a_folder to 6 -- ***
- end if
- set o's csvText to {} -- empty this property to not save his contents when the script quit ***
- end if
- end if
- end if
- on error error_message
- --display dialog error_message buttons {"OK"} default button 1
- end try
- end KeyworderChecker
- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- on CorrectRatio(t_path)
- tell application "Finder"
- set label index of document file t_path to 6
- end tell
- end CorrectRatio
- on CheckRatio(t_path)
- tell application "Finder"
- set label index of document file t_path to 5
- end tell
- end CheckRatio
- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- on getCSVpath(fName) -- return the path of the CSV file, and the week number
- set {tid, text item delimiters} to {text item delimiters, {"_"}}
- set TheFullName to text 1 thru text item -2 of fName
- log "fname handler " & fName -- remove the last underscore and characters after it
- set text item delimiters to tid
- try
- set CSV_Location to "" -- if not exists then the path will be empty
- try
- set CSV_Location to ((path to home folder as string) & "Dropbox:" & "keywords:" & TheFullName & ".csv") as alias
- on error
- set CSV_Location to ((path to startup disk as string) & "Volumes:CloudStorage:Dropbox:Keywords:" & TheFullName & ".csv") as alias
- end try
- end try
- return {CSV_Location, text -2 thru -1 of TheFullName}
- end getCSVpath
- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- on findNameInCsv(f)
- (*
- string f : HFS path of file
- return boolean : true if csv file is modified, false otherwise
- *)
- set fName to _split(":", f)'s item -1 -- filename
- tell _split(".", fName)
- if (count) > 1 then -- extensions exists
- set stem to "" & items 1 thru -2 -- name stem
- set ext to "." & item -1 -- name extension including leading period
- else
- set stem to fName
- set ext to ""
- end if
- end tell
- tell _split("_", stem)
- if (count) > 1 then
- set base to "" & items 1 thru -2 -- name stem w/o suffix
- set suffix to "_" & item -1 -- suffix including leading _
- else
- set base to stem -- name stem w/o suffix
- set suffix to ""
- end if
- end tell
- --log {fname, stem, ext, base, suffix} -- # for test
- set _modified to false
- set _has_suffix to suffix ≠ ""
- set n to base & ext
- set tc to count o's csvText
- repeat with i from 1 to tc
- set t to o's csvText's item i
- if not _has_suffix and t starts with n or _has_suffix and (t starts with n or t starts with "✔," & n) then -- found
- if not _has_suffix then
- set item i of o's csvText to "✔," & t -- modify csvText
- set _modified to true
- end if
- if t starts with "✔," then
- set delta to 1 -- field offset (this is required because field length in record is not constant)
- else
- set delta to 0
- end if
- set x4 to ""
- tell _split(",", t)
- try
- set x2 to (item (2 + delta) & suffix) --get original name
- set x3 to (item (3 + delta) & suffix) --get alternative Sku
- set x4 to item (4 + delta) -- get the keyword
- try
- set x6 to item (5 + delta) --get AIR OR SEA RECORD
- on error
- set x6 to ""
- end try
- end try
- end tell
- --log o's csvText -- # for test
- log {x2, x3, x4, x6, suffix} -- # for test
- set modDate to (do shell script "/usr/local/bin/exiftool -s3 -q -filemodifydate " & quoted form of POSIX path of f)
- -- exiftool add the keywords to EXIF
- if stem begins with "GT" then
- if suffix is equal to "" then
- set GTsuffix to "01"
- else
- try
- set GTsuffix to ("0" & (character 2 of suffix)) as text
- set x3 to ((characters 1 thru 12 of x3) & GTsuffix as text)
- on error errMsg
- log {"errMsg = " & errMsg}
- end try
- end if
- end if
- log {"x3_" & x3}
- try
- --if x4 ≠ "" then
- do shell script "/usr/local/bin/exiftool -a -api PNGEarlyXMP -overwrite_original_in_place -XMP:subject=" & (quoted form of x4) & " -headline=" & (quoted form of x2) & " -instructions=" & (quoted form of x3) & " -source=" & (quoted form of x6) & " " & quoted form of POSIX path of f
- --end if
- on error errMsg
- log {"errMsg " & errMsg}
- end try
- do shell script "/usr/local/bin/exiftool -api PNGEarlyXMP -overwrite_original_in_place -filemodifydate=" & (quoted form of modDate) & " " & quoted form of POSIX path of f
- end if
- end repeat
- return _modified
- end findNameInCsv
- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- on _split(d, t)
- (*
- string or list d : separator(s)
- string t : source string
- return list : t splitted by d
- *)
- local astid0, tt
- try
- set {astid0, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {} & d}
- set tt to t's text items
- set AppleScript's text item delimiters to astid0
- on error errs number errn
- set AppleScript's text item delimiters to astid0
- error errs number errn
- end try
- return tt
- end _split
- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- on write_to_file(the_file, tList) -- update CSV file ***
- set n to 0
- set n1 to 0
- set tc to count o's csvText
- 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 item i of o's csvText 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's csvText) as text -- convert list of lines to text
- set text item delimiters to tid
- try
- set openfile to open for access the_file with write permission
- set eof of openfile to 0
- write the_data to openfile starting at 0 as «class utf8»
- close access openfile
- on error
- try
- close access the_file
- end try
- end try
- return n1 = n -- if the number of lines equal the numbers of check marks
- end write_to_file
RAW Paste Data
