SHARE
TWEET

Untitled

Quiff May 27th, 2016 66 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.                
  81.                
  82.                
  83.                 if existsCSV then set o's csvText to paragraphs of (read checkListFile as «class utf8») -- get the contents of the CSV file ***
  84.                 launch application "Image Events" -- ***
  85.                 repeat with i from 1 to the count of filesOfFolder
  86.                     set t_path to (item i of filesOfFolder) as string
  87.                     tell application "Image Events"
  88.                         set t_image to open file t_path
  89.                         copy the dimensions of t_image to {xwidth, yheight}
  90.                         close t_image
  91.                     end tell
  92.                    
  93.                    
  94.                    
  95.                    
  96.                    
  97.                    
  98.                    
  99.                     if xwidth / yheight is greater than Ratio - 1.0E-3 and xwidth / yheight is less than Ratio + 1.0E-3 then
  100.                        
  101.                         my CorrectRatio(t_path)
  102.                     else
  103.                         my CheckRatio(t_path)
  104.                     end if
  105.                    
  106.                     -- *** find this name in CSV text ***
  107.                     if existsCSV then
  108.                         set {_found, _modified} to my findNameInCsv(t_path)
  109.                         set isChanged to _modified --  a check mark was added in CSV text ***
  110.                         if not _found then
  111.                             -- change label to orange
  112.                             tell application "Finder" to set label index of file t_path to 1
  113.                         end if
  114.                     end if
  115.                    
  116.                 end repeat
  117.                 if existsCSV then
  118.                     if isChanged then -- CSV file must be updated ***
  119.                         set r to my write_to_file(checkListFile, o's csvText) -- *** update CSV file ***
  120.                         -- *** if r is true, the checkList is complete so change the folder label to green ***
  121.                         if r then tell application "Finder" to set label index of a_folder to 6 -- ***
  122.                     end if
  123.                    
  124.                    
  125.                     set o's csvText to {} -- empty this property to not save his contents when the script quit ***
  126.                 end if
  127.             end if
  128.         end if
  129.     on error error_message
  130.         --display dialog error_message buttons {"OK"} default button 1
  131.        
  132.        
  133.        
  134.     end try
  135.    
  136.    
  137. end KeyworderChecker
  138.  
  139. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  140. on CorrectRatio(t_path)
  141.    
  142.     tell application "Finder"
  143.         set label index of document file t_path to 6
  144.     end tell
  145. end CorrectRatio
  146.  
  147. on CheckRatio(t_path)
  148.    
  149.     tell application "Finder"
  150.         set label index of document file t_path to 5
  151.     end tell
  152. end CheckRatio
  153.  
  154.  
  155.  
  156. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  157. on getCSVpath(fName) -- return the path of the CSV file, and the week number
  158.     set {tid, text item delimiters} to {text item delimiters, {"_"}}
  159.     set TheFullName to text 1 thru text item -2 of fName
  160.     log "fname handler " & fName -- remove  the last underscore and characters after it
  161.    
  162.    
  163.     set text item delimiters to tid
  164.     try
  165.         set CSV_Location to "" -- if not exists then the path will be empty
  166.         try
  167.             set CSV_Location to ((path to home folder as string) & "Dropbox:" & "keywords:" & TheFullName & ".csv") as alias
  168.         on error
  169.            
  170.             set CSV_Location to ((path to startup disk as string) & "Volumes:CloudStorage:Dropbox:Keywords:" & TheFullName & ".csv") as alias
  171.         end try
  172.        
  173.     end try
  174.     return {CSV_Location, text -2 thru -1 of TheFullName}
  175. end getCSVpath
  176. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  177. on findNameInCsv(f)
  178.     (*
  179.         string f : HFS path of file
  180.          return list : {_found, _modified}   -- ###### MODIFIED ######
  181.          (boolean) _found    : true if f's name is found in csv file, false otherwise
  182.             (boolean) _modified : true if csv file is modified, false otherwise
  183.     *)
  184.     set fName to _split(":", f)'s item -1 -- filename
  185.     tell _split(".", fName)
  186.         if (count) > 1 then -- extensions exists
  187.             set stem to "" & items 1 thru -2 -- name stem
  188.             set ext to "." & item -1 -- name extension including leading period
  189.         else
  190.             set stem to fName
  191.             set ext to ""
  192.         end if
  193.     end tell
  194.     tell _split("_", stem)
  195.         if (count) > 1 then
  196.             set base to "" & items 1 thru -2 -- name stem w/o suffix
  197.             set suffix to "_" & item -1 -- suffix including leading _
  198.         else
  199.             set base to stem -- name stem w/o suffix
  200.             set suffix to ""
  201.         end if
  202.     end tell
  203.     --log {fname, stem, ext, base, suffix} -- # for test
  204.    
  205.     set _modified to false
  206.     set _found to false -- ###### ADDED ######
  207.     set _has_suffix to suffix ≠ ""
  208.     set n to base & ext
  209.     set tc to count o's csvText
  210.    
  211.     repeat with i from 1 to tc
  212.         set t to o's csvText's item i
  213.         set _found to _found or (t starts with n or t starts with "✔," & n) -- ###### ADDED ######
  214.         if not _has_suffix and t starts with n or _has_suffix and (t starts with n or t starts with "✔," & n) then -- found
  215.             if not _has_suffix then
  216.                 set item i of o's csvText to "✔," & t -- modify csvText
  217.                 set _modified to true
  218.             end if
  219.             if t starts with "✔," then
  220.                 set delta to 1 -- field offset (this is required because field length in record is not constant)
  221.             else
  222.                 set delta to 0
  223.             end if
  224.             set x4 to ""
  225.             tell _split(",", t)
  226.                 try
  227.                     set x2 to (item (2 + delta) & suffix) --get original name
  228.                     set x3 to (item (3 + delta) & suffix) --get alternative Sku
  229.                     set x4 to item (4 + delta) -- get the keyword
  230.                     try
  231.                         set x6 to item (5 + delta) --get AIR OR SEA RECORD
  232.                     on error
  233.                         set x6 to ""
  234.                     end try
  235.                 end try
  236.             end tell
  237.             --log o's csvText -- # for test
  238.            
  239.             log {x2, x3, x4, x6, suffix} -- # for test
  240.             set modDate to (do shell script "/usr/local/bin/exiftool -s3 -q -filemodifydate " & quoted form of POSIX path of f)
  241.            
  242.            
  243.            
  244.             -- exiftool add the keywords to EXIF
  245.             if stem begins with "GT" then
  246.                 if suffix is equal to "" then
  247.                     set GTsuffix to "01"
  248.                 else
  249.                     try
  250.                         set GTsuffix to ("0" & (character 2 of suffix)) as text
  251.                         set x3 to ((characters 1 thru 12 of x3) & GTsuffix as text)
  252.                     on error errMsg
  253.                         log {"errMsg = " & errMsg}
  254.                     end try
  255.                 end if
  256.             end if
  257.             log {"x3_" & x3}
  258.             try
  259.                 --if x4 ≠ "" then
  260.                 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
  261.                 --end if
  262.             on error errMsg
  263.                 log {"errMsg " & errMsg}
  264.             end try
  265.             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
  266.            
  267.         end if
  268.     end repeat
  269.     return {_found, _modified} -- ###### MODIFIED #######
  270.    
  271. end findNameInCsv
  272. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  273. on _split(d, t)
  274.     (*
  275.         string or list d : separator(s)
  276.         string t : source string
  277.         return list : t splitted by d
  278.     *)
  279.     local astid0, tt
  280.     try
  281.         set {astid0, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {} & d}
  282.         set tt to t's text items
  283.         set AppleScript's text item delimiters to astid0
  284.     on error errs number errn
  285.         set AppleScript's text item delimiters to astid0
  286.         error errs number errn
  287.     end try
  288.     return tt
  289. end _split
  290. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  291. on write_to_file(the_file, tList) -- update CSV file ***
  292.     set n to 0
  293.     set n1 to 0
  294.     set tc to count o's csvText
  295.     repeat with i from 1 to tc --- ** move lines with check mark to the bottom **
  296.         set L to item i of o's csvText
  297.         if L is not "" then -- not a blank lines
  298.             set n1 to n1 + 1 -- count this valid line
  299.             if "✔" is in L then
  300.                 set n to n + 1 -- count this check mark
  301.                 set end of o's csvText to L
  302.                 set item i of o's csvText to missing value
  303.             end if
  304.         else
  305.             set item i of o's csvText to missing value -- remove this blank lines
  306.         end if
  307.     end repeat
  308.     set {tid, text item delimiters} to {text item delimiters, {return}}
  309.     set the_data to (text of o's csvText) as text -- convert list of lines to text
  310.     set text item delimiters to tid
  311.     try
  312.         set openfile to open for access the_file with write permission
  313.         set eof of openfile to 0
  314.         write the_data to openfile starting at 0 as «class utf8»
  315.         close access openfile
  316.     on error
  317.         try
  318.             close access the_file
  319.         end try
  320.     end try
  321.     return n1 = n -- if the number of lines equal the numbers of check marks
  322. end write_to_file
RAW Paste Data
Top