SHARE
TWEET

Untitled

Quiff May 19th, 2016 (edited) 80 Never
  1. property type_list : {"TIFF", "JPEG", "PNG", "PICT"}
  2. property extension_list : {"tif", "tiff", "jpg", "jpeg", "png", "pict", "psd"}
  3.  
  4.  
  5. set fileMatrix to {¬
  6.     {BrandInitials:"BHS", theRatios:{1348 / 1832}}, ¬
  7.     {BrandInitials:"Boots", theRatios:{1196 / 1300}}, ¬
  8.     {BrandInitials:"Burton", theRatios:{1020 / 1385}}, ¬
  9.     {BrandInitials:"Castro", theRatios:{1}}, ¬
  10.     {BrandInitials:"Daisy", theRatios:{1280 / 1740}}, ¬
  11.     {BrandInitials:"Diva", theRatios:{1000 / 1360}}, ¬
  12.     {BrandInitials:"Dorothy", theRatios:{1000 / 1360}}, ¬
  13.     {BrandInitials:"Freedom", theRatios:{2 / 3}}, ¬
  14.     {BrandInitials:"GinaT", theRatios:{19 / 23}}, ¬
  15.     {BrandInitials:"Glue", theRatios:{2 / 3}}, ¬
  16.     {BrandInitials:"KidsBHS", theRatios:{1348 / 1832}}, ¬
  17.     {BrandInitials:"KidsMatalan", theRatios:{1000 / 1400}}, ¬
  18.     {BrandInitials:"Matalan", theRatios:{1000 / 1400}}, ¬
  19.     {BrandInitials:"Mikey", theRatios:{1500 / 2000}}, ¬
  20.     {BrandInitials:"Norstrom", theRatios:{2 / 3}}, ¬
  21.     {BrandInitials:"Tally", theRatios:{806 / 1212}}, ¬
  22.     {BrandInitials:"Topman", theRatios:{1020 / 1385}}, ¬
  23.     {BrandInitials:"V&D", theRatios:{1}}, ¬
  24.     {BrandInitials:"Wallis", theRatios:{2 / 3}}, ¬
  25.     {BrandInitials:"WE", theRatios:{1}}, ¬
  26.     {BrandInitials:"Press", theRatios:{1000 / 1410}}, ¬
  27.     {BrandInitials:"Season", theRatios:{1000 / 1410}}}
  28.  
  29. script o -- ***
  30.     property csvText : {}
  31. end script
  32. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  33. try
  34.     tell application "Finder" to set CompletedFolder to folder (((path to home folder) as text) & "Dropbox:CompletedFolders")
  35. on error
  36.     tell application "Finder" to set CompletedFolder to folder (((path to startup disk) as text) & "Volumes:CloudStorage:Dropbox:CompletedFolders:")
  37. end try
  38.  
  39. repeat with matrixItem in fileMatrix
  40.     my KeyworderChecker((BrandInitials of matrixItem), (theRatios of the matrixItem), CompletedFolder)
  41. end repeat
  42.  
  43. on KeyworderChecker(BrandName, Ratio, CompletedFolder)
  44.    
  45.     try
  46.         tell application "Finder" to set the_Folders to (get every folder of CompletedFolder whose name starts with BrandName)
  47.        
  48.         if the_Folders is not {} then
  49.             set a_folder to item 1 of the_Folders
  50.             tell application "Finder"
  51.                
  52.                
  53.                 set filesOfFolder to every file of the a_folder whose ¬
  54.                     file type is in the type_list or name extension is in the extension_list
  55.                 set fName to name of a_folder -- to get the basename of the CSV file ***
  56.                
  57.                
  58.             end tell
  59.            
  60.            
  61.             if BrandName is equal to "KidsBHS" then
  62.                
  63.                 set ThewkNumber to (text 9 thru -5 of fName)
  64.                 set fName to "Kids_" & ThewkNumber & "_PSD"
  65.             end if
  66.            
  67.             if BrandName is equal to "KidsMatalan" then
  68.                 set ThewkNumber to (text 13 thru -5 of fName)
  69.                 set fName to "Kids_" & ThewkNumber & "_PSD"
  70.                 log "fname " & fName
  71.             end if
  72.            
  73.            
  74.            
  75.            
  76.             if filesOfFolder is not {} then -- ***
  77.                 set isChanged to false -- ***
  78.                 set {checkListFile} to my getCSVpath(fName) -- get the CSV path and the week number
  79.                 set existsCSV to checkListFile is not ""
  80.                 --if existsCSV then set o's csvText to paragraphs of (read checkListFile as «class utf8») -- get the contents of the CSV file ***
  81.                
  82.                 --remove the Tick.✔,
  83.                 (*set stringToFind to "✔,"
  84.                 set stringToReplace to ""
  85.                
  86.                 set theContent to read checkListFile as «class utf8»
  87.                 set {oldTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, stringToFind}
  88.                 set ti to every text item of theContent
  89.                 set AppleScript's text item delimiters to stringToReplace
  90.                 set newContent to ti as string
  91.                 set AppleScript's text item delimiters to oldTID
  92.                 try
  93.                     set fd to open for access checkListFile with write permission
  94.                     set eof of fd to 0
  95.                     write newContent to fd as «class utf8»
  96.                     close access fd
  97.                 on error
  98.                     close access checkListFile
  99.                 end try
  100.                 *)
  101.                 --
  102.                
  103.                
  104.                
  105.                 if existsCSV then set o's csvText to paragraphs of (read checkListFile as «class utf8») -- get the contents of the CSV file ***
  106.                 launch application "Image Events" -- ***
  107.                 repeat with i from 1 to the count of filesOfFolder
  108.                     set t_path to (item i of filesOfFolder) as string
  109.                    
  110.                     tell application "Image Events"
  111.                         set t_image to open file t_path
  112.                         copy the dimensions of t_image to {xwidth, yheight}
  113.                         close t_image
  114.                     end tell
  115.                    
  116.                     -- *** find this name in CSV text ***
  117.                     -- ###### MODIFIED ######
  118.                     if existsCSV then
  119.                         set {_found, _modified} to my findNameInCsv(t_path)
  120.                         set isChanged to _modified --  a check mark was added in CSV text ***
  121.                         if not _found then
  122.                             -- change label to orange
  123.                             tell application "Finder" to set label index of file t_path to 1
  124.                         end if
  125.                     end if
  126.                    
  127.                    
  128.                    
  129.                    
  130.                    
  131.                     if xwidth / yheight is greater than Ratio - 1.0E-3 and xwidth / yheight is less than Ratio + 1.0E-3 then
  132.                        
  133.                         my CorrectRatio(t_path)
  134.                     else
  135.                         my CheckRatio(t_path)
  136.                     end if
  137.                    
  138.                     -- *** find this name in CSV text ***
  139.                     if existsCSV and (my findNameInCsv(t_path)) then
  140.                         set isChanged to true --  a check mark was added in CSV text ***
  141.                     else if existsCSV and (my findNameInCsv(t_path)) is false then
  142.                         --tell application "Finder" to set label index of document file t_path to 1
  143.                     end if
  144.                    
  145.                 end repeat
  146.                 if existsCSV then
  147.                     if isChanged then -- CSV file must be updated ***
  148.                         set r to my write_to_file(checkListFile, o's csvText) -- *** update CSV file ***
  149.                         -- *** if r is true, the checkList is complete so change the folder label to green ***
  150.                         if r then tell application "Finder" to set label index of a_folder to 6 -- ***
  151.                     end if
  152.                    
  153.                    
  154.                     set o's csvText to {} -- empty this property to not save his contents when the script quit ***
  155.                 end if
  156.             end if
  157.         end if
  158.     on error error_message
  159.         --display dialog error_message buttons {"OK"} default button 1
  160.        
  161.        
  162.        
  163.     end try
  164.    
  165.    
  166. end KeyworderChecker
  167.  
  168. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  169. on CorrectRatio(t_path)
  170.    
  171.     tell application "Finder"
  172.         set label index of document file t_path to 6
  173.     end tell
  174. end CorrectRatio
  175.  
  176. on CheckRatio(t_path)
  177.    
  178.     tell application "Finder"
  179.         set label index of document file t_path to 5
  180.     end tell
  181. end CheckRatio
  182.  
  183.  
  184.  
  185. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  186. on getCSVpath(fName) -- return the path of the CSV file, and the week number
  187.     set {tid, text item delimiters} to {text item delimiters, {"_"}}
  188.     set TheFullName to text 1 thru text item -2 of fName
  189.     log "fname handler " & fName -- remove  the last underscore and characters after it
  190.    
  191.    
  192.     set text item delimiters to tid
  193.     try
  194.         set CSV_Location to "" -- if not exists then the path will be empty
  195.         try
  196.             set CSV_Location to ((path to home folder as string) & "Dropbox:" & "keywords:" & TheFullName & ".csv") as alias
  197.         on error
  198.            
  199.             set CSV_Location to ((path to startup disk as string) & "Volumes:CloudStorage:Dropbox:Keywords:" & TheFullName & ".csv") as alias
  200.         end try
  201.        
  202.     end try
  203.     return {CSV_Location, text -2 thru -1 of TheFullName}
  204. end getCSVpath
  205. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  206. on findNameInCsv(f)
  207.     (*
  208.         string f : HFS path of file
  209.         return list : {_found, _modified}   -- ###### MODIFIED ######
  210.             (boolean) _found    : true if f's name is found in csv file, false otherwise
  211.             (boolean) _modified : true if csv file is modified, false otherwise
  212.     *)
  213.     set fName to _split(":", f)'s item -1 -- filename
  214.     tell _split(".", fName)
  215.         if (count) > 1 then -- extensions exists
  216.             set stem to "" & items 1 thru -2 -- name stem
  217.             set ext to "." & item -1 -- name extension including leading period
  218.         else
  219.             set stem to fName
  220.             set ext to ""
  221.         end if
  222.     end tell
  223.     tell _split("_", stem)
  224.         if (count) > 1 then
  225.             set base to "" & items 1 thru -2 -- name stem w/o suffix
  226.             set suffix to "_" & item -1 -- suffix including leading _
  227.         else
  228.             set base to stem -- name stem w/o suffix
  229.             set suffix to ""
  230.         end if
  231.     end tell
  232.     --log {fname, stem, ext, base, suffix} -- # for test
  233.    
  234.     set _modified to false
  235.     set _found to false -- ###### ADDED ######
  236.     set _has_suffix to suffix ≠ ""
  237.    
  238.     set n to base & ext
  239.     set tc to count o's csvText
  240.    
  241.     repeat with i from 1 to tc
  242.         set t to o's csvText's item i
  243.         set _found to _found or (t starts with n or t starts with "✔," & n) -- ###### ADDED ######
  244.         if not _has_suffix and t starts with n or _has_suffix and (t starts with n or t starts with "✔," & n) then -- found
  245.             if not _has_suffix then
  246.                 set item i of o's csvText to "✔," & t -- modify csvText
  247.                 set _modified to true
  248.             end if
  249.             if t starts with "✔," then
  250.                 set delta to 1 -- field offset (this is required because field length in record is not constant)
  251.             else
  252.                 set delta to 0
  253.             end if
  254.             set x4 to ""
  255.             tell _split(",", t)
  256.                 try
  257.                     set x2 to (item (2 + delta) & suffix) --get original name
  258.                     set x3 to (item (3 + delta) & suffix) --get alternative Sku
  259.                     set x4 to item (4 + delta) -- get the keyword
  260.                     try
  261.                         set x6 to item (5 + delta) --get AIR OR SEA RECORD
  262.                     on error
  263.                         set x6 to ""
  264.                     end try
  265.                 end try
  266.             end tell
  267.             --log o's csvText -- # for test
  268.            
  269.             log {x2, x3, x4, x6, suffix} -- # for test
  270.             set modDate to (do shell script "/usr/local/bin/exiftool -s3 -q -filemodifydate " & quoted form of POSIX path of f)
  271.            
  272.            
  273.            
  274.             -- exiftool add the keywords to EXIF
  275.             if stem begins with "GT" then
  276.                 if suffix is equal to "" then
  277.                     set GTsuffix to "01"
  278.                 else
  279.                     try
  280.                         set GTsuffix to ("0" & (character 2 of suffix)) as text
  281.                         set x3 to ((characters 1 thru 12 of x3) & GTsuffix as text)
  282.                     on error errMsg
  283.                         log {"errMsg = " & errMsg}
  284.                     end try
  285.                 end if
  286.             end if
  287.             log {"x3_" & x3}
  288.             try
  289.                 --if x4 ≠ "" then
  290.                 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
  291.                 --end if
  292.             on error errMsg
  293.                 log {"errMsg " & errMsg}
  294.             end try
  295.             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
  296.            
  297.         end if
  298.     end repeat
  299.    
  300.    
  301.     return {_found, _modified}
  302. end findNameInCsv
  303. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  304. on _split(d, t)
  305.     (*
  306.         string or list d : separator(s)
  307.         string t : source string
  308.         return list : t splitted by d
  309.     *)
  310.     local astid0, tt
  311.     try
  312.         set {astid0, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {} & d}
  313.         set tt to t's text items
  314.         set AppleScript's text item delimiters to astid0
  315.     on error errs number errn
  316.         set AppleScript's text item delimiters to astid0
  317.         error errs number errn
  318.     end try
  319.     return tt
  320. end _split
  321. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  322. on write_to_file(the_file, tList) -- update CSV file ***
  323.     set n to 0
  324.     set n1 to 0
  325.     set tc to count o's csvText
  326.     repeat with i from 1 to tc --- ** move lines with check mark to the bottom **
  327.         set L to item i of o's csvText
  328.         if L is not "" then -- not a blank lines
  329.             set n1 to n1 + 1 -- count this valid line
  330.             if "✔" is in L then
  331.                 set n to n + 1 -- count this check mark
  332.                 set end of o's csvText to L
  333.                 set item i of o's csvText to missing value
  334.             end if
  335.         else
  336.             set item i of o's csvText to missing value -- remove this blank lines
  337.         end if
  338.     end repeat
  339.     set {tid, text item delimiters} to {text item delimiters, {return}}
  340.     set the_data to (text of o's csvText) as text -- convert list of lines to text
  341.     set text item delimiters to tid
  342.     try
  343.         set openfile to open for access the_file with write permission
  344.         set eof of openfile to 0
  345.         write the_data to openfile starting at 0 as «class utf8»
  346.         close access openfile
  347.     on error
  348.         try
  349.             close access the_file
  350.         end try
  351.     end try
  352.     return n1 = n -- if the number of lines equal the numbers of check marks
  353. end write_to_file
RAW Paste Data
Top