PDA

View Full Version : Folder Action to spotlight and copy




tedesco24
Feb 14, 2013, 11:17 AM
I'm hoping this is a better way to find help vs hours spent on google.

I want to set up a Folder Action to accomplish the following:

I scan a pdf to my desktop, drag and drop the pdf to the subfolder labeled "2013"

The Folder Action should fire up and:
1. Add a spotlight comment
2. Copy the modified pdf, with the updated spotlight comment, to an external drive.
3. Copy the modified pdf, with the updated spotlight comment, to the cloud (this is icing on the cake, so not vital to the workflow)

I've created a few Folder Actions but can't quite get the workflow to be consistent.

Thanks



chown33
Feb 14, 2013, 03:06 PM
Post what you have now.

Please explain what you mean by "can't quite get the workflow to be consistent". Do you mean it consistently doesn't work? It inconsistently works? Some sub-part works inconsistently? The copied data is inconsistent (i.e. not identical) to the original?

tedesco24
Feb 14, 2013, 09:54 PM
Thank you for taking an interest and trying to help.

Here is my current Folder Action:

Folder Action receives files and folders from (DESKTOP FOLDER)
1. Set Spotlight Comments for Finder Items
Append to Existing Docs and Show This Action are checked

2. Find Finder Items (IGNORE INPUT)
Search (DESKTOP FOLDER)
ALL of the following are true
ANY Content
3. Filter Finder Items
All of the following are true
Kind is not Folder (did this so that the file copies, not the folder)
Date created is today (only want to copy the most recent file, not the entire folder contents)
4. Copy Finder Items
To (EXTERNAL DRIVE)

The only problem I'm having is the Spotlight Comments are not showing up on the file that is landing on my External Drive. The file gets there but the Spotlight Comments are missing. Any ideas why?

chown33
Feb 14, 2013, 11:25 PM
I don't know why the comments wouldn't be on the copy. Maybe it's the timing. That is, the copy occurs too close on the heels of setting the comment, so either Finder fails to copy it, or Spotlight loses it.

A possible solution is to not add the comment to the original file in Desktop folder, but to add the comment on the copy, after it's been copied, on the external HD.

kryten2
Feb 15, 2013, 10:07 AM
Why don't you try a simple workflow with only the Set Spotlight Comments for Finder Items action. Attach it to your 2013 folder and drop a pdf in it. See if the comment is set. We are talking about Automator workflows here right? Also what version of OS X are you running? I'm guessing it's Leopard.

tedesco24
Feb 15, 2013, 01:41 PM
Chown...I also was thinking it was a timing issue. I messed around and added a Pause action. That didn't solve the problem.

Kryten...I'm running ML. I've re-built the workflow step by step so I can hopefully figure out where the error is. The only thing not working is retaining the Comments on the file being copied to the external drive.

Im about 90% finished with a Hazel workflow to handle this but I'd prefer a Folder Action that I can slap on all of my folders versus setting up Hazel rules for all of the folders that need the workflow (about 40 folders).

chown33
Feb 15, 2013, 01:59 PM
A file's comment is stored in a named xattr. You can list the names with the command 'ls -l@ filename'. Then you can use the 'xattr' command to show the value of the xattr, write it, delete it, etc. See 'man xattr'.

I would break this down into individual steps, starting with putting the comment on the file. Confirm that works using an Automator action that runs a shell script, and in that script use 'xattr' with appropriate options to list and show whether the file really has a Finder comment. If the xattr for the comment doesn't show up, then you've isolated the failing step.

Since 'xattr' can also write xattrs, you could use it to attach a comment, but it would take quite some effort to get the data in the right format (a binary plist, or "bplist") to work with 'xattr' as a shell command.


BTW, if it's not a timing issue, maybe it's a caching issue. The 'sync' command will flush everything to disk. I don't think this is the cause, but it's worth a shot.

Personally, I think Finder is just out of sync. It doesn't see the change, or doesn't think it needs to resync, so it doesn't copy the xattr. I've seen plenty of Finder bugs in Mtn Lion where things change in the file-system but Finder doesn't see it. This could easily be one of those.

If the 'xattr' cmd does show a comment, but Finder still fails to copy it, you should be able to use the 'cp' command to copy. Since at least OS X 10.6, 'cp' has been aware of resource-forks, xattrs, etc. and will copy them. Another command that's aware of these extras is 'ditto', so 'man ditto'.

tedesco24
Feb 17, 2013, 11:51 AM
whoa, you went all Nick Burns on me.

My desired Folder Action seems rather basic, I'm really surprised there isn't a pre-made Action to accomplish this.

Anyways, it seems the more I play with this, the more frustrating it gets. Right now my spotlight comments are only sticking with the file once it lands in the Desktop folder. But once I add any additional action to the workflow (MOVE THE FINDER ITEM or COPY THE FINDER ITEM or RENAME THE FINDER ITEM, etc), the comments disappear.

Any suggestions on how to build this from the ground up? Could it be related to the file is being moved from a root folder to a subfolder? I've only been using the ACTION commands, should I look into using VARIABLES?

chown33
Feb 17, 2013, 01:22 PM
My desired Folder Action seems rather basic, I'm really surprised there isn't a pre-made Action to accomplish this.

I'm not surprised at all. You described the sub-actions yourself:
1. Add a spotlight comment
2. Copy the modified pdf, with the updated spotlight comment, to an external drive.
3. Copy the modified pdf, with the updated spotlight comment, to the cloud.

Each of those things is an Action. Why would there be a pre-made one that did them all?


FWIW, the non-copying of the comment seems like a bug to me. Trying to wish it away won't work. You have to come up with a way to work around it if you hope to accomplish the final goal. It's not magic; it's programming.


Any suggestions on how to build this from the ground up? Could it be related to the file is being moved from a root folder to a subfolder?

Since the problem seems to be that a comment is not being copied, try doing the copying first, and the comment attachment last.

Your original sequence:

Add a spotlight comment
Copy the modified pdf, with the updated spotlight comment, to an external drive.
Copy the modified pdf, with the updated spotlight comment, to the cloud.


Rearranged sequence:

Copy the pdf to an external drive.
Add a spotlight comment to the copy.
Copy the modified pdf, with the updated spotlight comment, to the cloud .


Or if you need the original to have the same comment:

Add a spotlight comment to the original.
Copy the pdf to an external drive (losing its comment).
Add a spotlight comment to the copy on the external drive.
Copy the original (or the duplicate on the external drive) to the cloud.



I've only been using the ACTION commands, should I look into using VARIABLES?

What do you think that will change?

If you've never used Automator variables, or don't know how they work, then I think learning them is a useful added skill. But if you're learning them in the hopes of magically fixing the bug, I suspect you'll be disappointed.

kryten2
Feb 17, 2013, 06:26 PM
Can you try this script. Run it from Script Editor.

set these_items to choose file with prompt "Choose file you wish to comment : " with multiple selections allowed without invisibles
repeat with anItem in these_items
tell application "Finder" to set {theComment, theContainer, theName} to {comment, container, name} of anItem
if theComment is "" then
set theNewComment to text returned of (display dialog "Folder : " & theContainer & return & "Name : " & theName & return & "Comment : " & theComment & return & return & "Type in the comment you would like to add to the " & theName & " file." with title "Adding Spotlight Comments" default answer "" buttons {"OK"} default button 1)
tell application "Finder" to set anItem's comment to theNewComment
set theDestination to (choose folder with prompt "Copy " & theName & " to : ")
tell application "Finder" to set theCopiedItem to duplicate anItem to theDestination
tell application "Finder" to set {theCopiedComment, theCopiedContainer, theCopiedName} to {comment, container, name} of theCopiedItem
display dialog "Folder : " & theCopiedContainer & return & "Name : " & theCopiedName & return & "Comment : " & theCopiedComment with title "Display Copied Item Spotlight Comments" buttons {"OK"} default button 1
else
display dialog "Folder : " & theContainer & return & "Name : " & theName & return & "Comment : " & theComment with title "Display Current Spotlight Comments" buttons {"OK"} default button 1
end if
end repeat

tedesco24
Feb 18, 2013, 07:45 AM
Chown and Kryten, thank you both for helping. I'll play with both solutions later today.

I've never programmed anything more complex than my VCR in the 80's, so this as all foreign to me. It's a "learn as I go" process.

Enjoy the day.

tedesco24
Feb 22, 2013, 09:07 AM
Thank you for the suggestions. I played around with them, added a few tweaks, and got the Folder Action to work as desired.

Here's the workflow for anybody that might be looking for something similar.

Folder Action receives files and folders added to "DESKTOP FOLDER A"

1. REPLACE TEXT - Show this action
2. SET SPOTLIGHT COMMENTS FOR FINDER ITEMS -append to existing-show this action
3. COPY FINDER ITEMS to "EXTERNAL DRIVE FOLDER A"
4. SET SPOTLIGHT COMMENTS FOR FINDER ITEMS - show this action- (DO NOT check "append" or you will have duplicate comments)

This works exactly as I needed it to.

I can scan a pdf to my desktop and give it a generic name such as "z 1". I'll then drop the pdf file in the appropriate folder on my desktop. This FOLDER ACTION recognizes the pdf and gives me the option to rename the file to something specific, add the spotlight comments, and copy the finished file to my external drive. The finished file ends up in both my desktop folder and external drive folder, perfectly synced with the name and comments! I imagine I could duplicate steps 3-4 and add another location (the cloud?) but that would be overkill for what I need.