AppleScript - Folder actions - add tag to file in workflow
Hi there
I'm no star in all this, I tried to create it myself, but after 2 days of trying to change all the parameters, I'm pulling a "hail mary" and hoping someone can tell me how it is done:
I'm running macOS Catalina.
The resources I already tried to incorporate (and some others, but they boil down to these 3, most of the time):
https://discussions.apple.com/thread/8267280?answerId=32971986022#32971986022
I have a workflow when I add a file to a folder:
- add specific tags
- upload to cloud
- create backup file
Since I ran into some Folder actions issues (eg: not running), I want to add some extra steps, so it would look like this:
- add specific tags (related to the folder)
- upload to cloud
- add tag "uploaded"
- create backup
- add tag "backup"
and if you would indulge me, let me stretch my request, just a little bit:
in step 1, would it be possible to
- add a tag, based on the first 4 digits of the filename?
- add a tag, based on the 5th and 6th digit of the filename (eg: 1 to 3 = 1, 4 to 6 = 2, 7 to 9 = 3, etc...)?
The code I have until now:
- create tags:
property ca : current application
property tagname : {"tag1", "tag2", "tag3"}
use framework "foundation"
use AppleScript version "2.4" -- Yosemite 10.10 or later required
use scripting additions
on run {input, parameters}
repeat with anItem in input
set_tag(anItem, tagname)
end repeat
return input
end run
on set_tag(theFile, atag)
set tagArray to ca's NSArray's arrayWithArray:atag
set fileURL to ca's |NSURL|'s fileURLWithPath:(POSIX path of theFile)
fileURL's setResourceValue:tagArray forKey:(ca's NSURLTagNamesKey) |error|:(missing value)
end set_tag
- upload to cloud
on run {input, parameters}
set targetNotebook to "notebook 1"
repeat with this_item in the input
set the item_info to info for this_item
# uncomment if you want to copy tags to evernote:
# set the_tags to paragraphs of (do shell script "mdls -raw -name kMDItemUserTags " & quoted form of POSIX path of this_item & " | sed 's/^[()]$//g' | ruby -ne 'each = $_.strip.end_with?(\",\") ? $_.strip[0...-1] : $_.strip; puts each if each != \"\"'"
tell application "Finder"
set createdFileDate to (the creation date of (this_item as alias))
set modFileDate to (the modification date of (this_item as alias))
end tell
tell application "Evernote"
launch
# uncomment if you want to copy tags to evernote:
# set theItem to create note from file this_item notebook targetNotebook tags the_tags created createdFileDate
set theItem to create note from file this_item notebook targetNotebook created createdFileDate
set (modification date of theItem) to modFileDate
end tell
end repeat
return input
end run
- copy Finder items is the third step, and is standard.
Thank you very much, in advance!