Returning a dropped file to its original location.

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

I read the most of the docs and searched the knowledge base / forum. Seems like what I want to do should be simple, but I can't figure it out.

All I want to do is have a monitored folder, and when I drag and drop file(s) in to that folder I want to apply some tags and then move the file(s) back to wherever it was dragged from (its original location).

So the rule I setup on my test folder was:
Code: Select all
If all of the following conditions are met
Any File

Do the following to the matched file or folder:
Add tags: tag1 tag2 tag3
[insert action here to move the file back to its original location]


Is this possible?

First I tried Move. That doesn't work because it only lets me specify a specific folder, but he original location could be anywhere.
Second I tried Sort into subfolder. I tried with pattern [source folder] and with [original source], but neither worked. I couldn't find good documentation about how exactly those patterns worked.

I'm basically just trying to hack myself a better UI for adding tags to files, because the one built into macOS is awful.

Thanks!
benbarry
 
Posts: 2
Joined: Mon Nov 01, 2021 2:11 pm

Hazel can't move stuff back because it has no idea where it came from. There's no mechanism or place where that info is recorded. The one exception I can think of is that Finder records that when putting stuff in the Trash, so that the "Put back" feature can work.
Mr_Noodle
Site Admin
 
Posts: 11193
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Bummer—seems like information that should be recorded.

However, last night I did find a non-Hazel solution to my problem. Using macOS Automator I was able to create Droplet Applications with some custom Applescript (Unfortunately, Automator doesn't have a standard action for handling tags). Because it's a droplet, it runs the action when files get dropped on to it, but the files are never moved from their original location. Works perfectly for my purposes.

Here is the code for anyone who finds this in the future. I cobbled this together from various responses by VikingOSX on the Apple Discussion boards.

Code: Select all
property ca : current application
property tagname : {"tag1", "tag2", "tag3", "tag4"}

use framework "Foundation"

use AppleScript version "2.4"
use scripting additions

on run {input, parameters}
    repeat with anItem in input
        add_tag(anItem, tagname)
    end repeat
    return
end run

on add_tag(theFile, atag)
    set fileURL to ca's |NSURL|'s fileURLWithPath:(POSIX path of theFile)
    set {theResult, theTags} to fileURL's getResourceValue:(reference) forKey:(ca's NSURLTagNamesKey) |error|:(missing value)
    if theTags is not missing value then
        set tagList to (theTags as list) & atag
        -- set will eliminate duplicate tag names
        set tagList to (ca's NSOrderedSet's orderedSetWithArray:tagList)'s allObjects()
        -- apply the additional tag(s) to the file/folder
        fileURL's setResourceValue:tagList forKey:(ca's NSURLTagNamesKey) |error|:(missing value)
    else
        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 if
   
    return
end add_tag


Thanks!
benbarry
 
Posts: 2
Joined: Mon Nov 01, 2021 2:11 pm


Return to Support

cron