Q: Adding Keywords
https://discussions.apple.com/thread/4609223?start=0&tstart=0
This is where I got to recently with a script many thanks to all that helped.
The issue I still have is that keywords don't get added to the files labelled AB12NW01234MU2MM_2
The way it works is to find the take the name of the file.
AB12NW01234MU2MM.psd (that is always 16 digits inc Extentsion)
It then checks against a CSV File,
AB12NW01234MU2MM.psd,AB12NW01234MU2MM,504123,Picture of a person,5
It then uses the line above to add the other data to the IPTC/Exif data of the file.
At the same time it still needs to add a checkmark to say that that image has been shot.
The above works with the script, however it does not cater for those images that have _2, _3, _4
With these it needs to do the same but only do it to add information to the file not add the tick mark.
Very difficult to perhaps understand the code, but I hope someone can help me. These are the handlers below
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 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 count o's csvText
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 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
Mac Pro, OS X Mavericks (10.9)
Posted on Nov 28, 2013 8:45 AM
Hello
The original code won't handle _2 suffix correctly because it is expecting _02 suffix. Another thing to note is that it is not appropriate to add new field of check mark to particular record in CSV because it results in mal-formed CSV which should have the same number of fields in every record.
Anyway, if I'm not mistaken, the following handler(s) will address the first problem, i.e., suffix processing. You need to replace the original findNameInCSV() handler with the new findNameInCSV() handler and the general purpose _split() handler. The statements other than those handlers in the code below are for testing.
script o
property csvText : {"AB12NW01234MU2MM.psd,AB12NW01234MU2MM,504123,Picture of a person,5"}
--property csvText : {"✔,AB12NW01234MU2MM.psd,AB12NW01234MU2MM,504123,Picture of a person,5"}
end script
set f to "path:to:AB12NW01234MU2MM_2.psd"
--set f to "path:to:AB12NW01234MU2MM.psd"
findNameInCSV(f)
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) --get original name
set x3 to item (3 + delta) --get alternative Sku
set x4 to item (4 + delta) -- get the keyword
try
set x6 to item (6 + 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} -- # for test
-- exiftool add the keywords to EXIF
if x4 ≠ "" 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 x6) & " " & quoted form of POSIX path of f
return _modified
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
By the way, it might be better to use more suitable handler name such as processNameInCSV() than findNameInCSV().
Hope this may help,
H
Posted on Dec 2, 2013 7:19 PM