SHARE
TWEET

Keyword CSV

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