The script below works perfectly fine if I run it from the macOS Script Editor - hashtags in my file get written to the Tags attribute of my manually selected file.
- Code: Select all
-- https://macscripter.net/viewtopic.php?pid=204309#p204309
-- haolesurferdude
-- 2020-11-06
-- on hazelProcessFile(theFile, inputAttributes)
set readFile to POSIX path of (choose file of type {"pdf", false})
-- set readFile to POSIX path of theFile
-- end hazelProcessFile
use scripting additions
use framework "Foundation"
property endChars : return & linefeed & space
set theText to do shell script "/usr/local/bin/pdftotext " & readFile & " -"
set theText to theText & return
set hashTagList to my getHashTags(theText)
my setTags:hashTagList forPath:readFile
on getHashTags(fileText)
set hashTagsList to {}
repeat until fileText does not contain "#"
set poundLoc to offset of "#" in fileText
set thisHashTag to ""
repeat with i from poundLoc to (count of fileText)
set thisChar to item i of fileText
if endChars does not contain thisChar then
set thisHashTag to thisHashTag & thisChar
else
copy thisHashTag to the end of the hashTagsList
set fileText to characters (poundLoc + (count of thisHashTag)) thru -1 of fileText as text
exit repeat
end if
end repeat
end repeat
return hashTagsList
end getHashTags
-- The following routines are by Shane Stanley
on returnTagsFor:posixPath -- get the tags
set aURL to current application's |NSURL|'s fileURLWithPath:posixPath -- make URL
set {theResult, theTags} to aURL's getResourceValue:(reference) forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
if theTags = missing value then return {} -- because when there are none, it returns missing value
return theTags as list
end returnTagsFor:
on setTags:tagList forPath:posixPath -- set the tags, replacing any existing
set aURL to current application's |NSURL|'s fileURLWithPath:posixPath -- make URL
aURL's setResourceValue:tagList forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
end setTags:forPath:
on addTags:tagList forPath:posixPath -- add to existing tags
set aURL to current application's |NSURL|'s fileURLWithPath:posixPath -- make URL
-- get existing tags
set {theResult, theTags} to aURL's getResourceValue:(reference) forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
if theTags ≠ missing value then -- add new tags
set tagList to (theTags as list) & tagList
set tagList to (current application's NSOrderedSet's orderedSetWithArray:tagList)'s allObjects() -- delete any duplicates
end if
aURL's setResourceValue:tagList forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
end addTags:forPath:
I receive errors trying to use the script as embedded and so I tried it as an external script. I understand this requires that I add "on hazelProcessFile(theFile, inputAttributes)" and "end hazelProcessFile".
Here's the external script, with the necessary hazel bits. Even though I confirm that the file matches (any file), the Finder tags are never updated. What do I need to change?
- Code: Select all
-- https://macscripter.net/viewtopic.php?pid=204309#p204309
-- haolesurferdude
-- 2020-11-06
on hazelProcessFile(theFile, inputAttributes)
-- set readFile to POSIX path of (choose file of type {"pdf", false})
set readFile POSIX path of theFile
end hazelProcessFile
use scripting additions
use framework "Foundation"
property endChars : return & linefeed & space
set theText to do shell script "/usr/local/bin/pdftotext " & readFile & " -"
set theText to theText & return
set hashTagList to my getHashTags(theText)
my setTags:hashTagList forPath:readFile
on getHashTags(fileText)
set hashTagsList to {}
repeat until fileText does not contain "#"
set poundLoc to offset of "#" in fileText
set thisHashTag to ""
repeat with i from poundLoc to (count of fileText)
set thisChar to item i of fileText
if endChars does not contain thisChar then
set thisHashTag to thisHashTag & thisChar
else
copy thisHashTag to the end of the hashTagsList
set fileText to characters (poundLoc + (count of thisHashTag)) thru -1 of fileText as text
exit repeat
end if
end repeat
end repeat
return hashTagsList
end getHashTags
-- The following routines are by Shane Stanley
on returnTagsFor:posixPath -- get the tags
set aURL to current application's |NSURL|'s fileURLWithPath:posixPath -- make URL
set {theResult, theTags} to aURL's getResourceValue:(reference) forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
if theTags = missing value then return {} -- because when there are none, it returns missing value
return theTags as list
end returnTagsFor:
on setTags:tagList forPath:posixPath -- set the tags, replacing any existing
set aURL to current application's |NSURL|'s fileURLWithPath:posixPath -- make URL
aURL's setResourceValue:tagList forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
end setTags:forPath:
on addTags:tagList forPath:posixPath -- add to existing tags
set aURL to current application's |NSURL|'s fileURLWithPath:posixPath -- make URL
-- get existing tags
set {theResult, theTags} to aURL's getResourceValue:(reference) forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
if theTags ≠ missing value then -- add new tags
set tagList to (theTags as list) & tagList
set tagList to (current application's NSOrderedSet's orderedSetWithArray:tagList)'s allObjects() -- delete any duplicates
end if
aURL's setResourceValue:tagList forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
end addTags:forPath: