Go Back   The macosxhints Forums > OS X Help Requests > AppleScript



Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
Old 02-26-2012, 01:58 PM   #1
frankeman90
Prospect
 
Join Date: Apr 2011
Posts: 8
Auto add spotlight comments to files in folders AND subfolders

Using automator to add spotlight comments to files in certain folders. (eg The files in folder RED ONE get spotlight commented with "RED ONE" automatically.

But now I need to tweak the applescript to add spotlight comments ALSO to content in SUBFOLDERS. (This way I only need to make an folder action for the top folder and not the 100:s of folders in that topfolder)

My folder action script looks like this (and works great, but just for this folder, not subfolders)


on run {input, parameters}



tell application "Finder"

repeat with this_item in input

set fname to name of container of this_item as text

set comment of this_item to (comment of this_item) & return & fname

end repeat

end tell

end run
frankeman90 is offline   Reply With Quote
Old 02-26-2012, 02:44 PM   #2
Red_Menace
All Star
 
Join Date: Mar 2006
Location: Littleton, Colorado, USA
Posts: 513
As long as you are using Automator, you can just put a Get Folder Contents action at the beginning of the workflow - note that this will also go into bundles.
__________________
MacBook Pro / OS X Mavericks (10.9.2) / Xcode 5.0.2 / [various (much) older stuff keeping dust off the shelves]
Red_Menace is offline   Reply With Quote
Old 02-27-2012, 01:39 AM   #3
frankeman90
Prospect
 
Join Date: Apr 2011
Posts: 8
Thought so too. But it does not work in this case. In example I have done exactly that.

Example:
FOLDER "RED" (gets assigned with folder action above)
> FILE "APPLE" (spotlight keyword "red" get attached to file)

---- FOLDER "BLUE" (inside folder RED)
---- > FILE "BANANA" (does not get any keywords attached)

Last edited by frankeman90; 02-27-2012 at 01:48 AM.
frankeman90 is offline   Reply With Quote
Old 02-27-2012, 02:04 AM   #4
NaOH
Hall of Famer
 
Join Date: Dec 2007
Posts: 3,749
I just created a Folder Action with Automator. The two-action creation involved just the Get Folder Contents action and the Set Spotlight Comments For Finder Items action. I then dropped a folder which had one document and a sub-folder with two items and everything in the folder had Spotlight comments added. The resulting moved-folder contents looked like this:
  • File A (received Spotlight Comments)
  • Sub-Folder (received Spotlight Comments)
    • File B (received Spotlight Comments)
    • File C (received Spotlight Comments)

Maybe I'm misunderstanding the goal here, but I think this should work.

Perhaps of use since you haven't discussed why you're applying Spotlight Comments, you may want to read the discussion about them beginning with Post #14 in this thread for some of the peculiarities of this feature.
NaOH is offline   Reply With Quote
Old 02-27-2012, 01:07 PM   #5
frankeman90
Prospect
 
Join Date: Apr 2011
Posts: 8
Thanks for your effort!

I tried your solution, and yes, the spotlight comments get added when I do as you did. I moved the content away from the folder and back again, and now the files get comments.

Problem is when I add a new file to a sub-folder no spotlight comments get added (just in top-folder). This might have something to do with me using the applescript code instead of the spotlight action for "Spotlight comments". But I cannot use that one. See:

What I want to accomplish is quite straight-forward:
• I have a library of graphics 1000's (eps files) sorted in folders and subfolders.
Looks like this
Top folder: "Graphics"
Subfolder1: "Silhouettes food" > Apple on table 1.eps

I would like the file "Apple on table 1.eps" to get spotlight commented with "Silhouettes food" (and even better also with "Graphics" but I dont know if thats possible) without needing to make a folder action for each folder in the folder "Graphics". (Only create a folder action for the top "Graphics" folder).

What I want to accomplish with this is to be able to make a quick search for "silhouettes food apple" and hit everything right away. This way I dont have to tag each file with all the keywords.

And yes i know that Spotlight comments may not be the safest way to ensure the information added will stay forever (as to the discussion in linked thread). But anyway I think my solution is good for now, and is easy to repeat if something would happen.

Do you think above is even possible?
frankeman90 is offline   Reply With Quote
Old 02-27-2012, 02:53 PM   #6
NaOH
Hall of Famer
 
Join Date: Dec 2007
Posts: 3,749
Yeah, I see the issue you're experiencing. I'm not certain how that could be resolved without having folder actions for each folder. Maybe it can somehow be done with AppleScript, but that would be well beyond my scripting skills.

Quote:
Originally Posted by frankeman90
I would like the file "Apple on table 1.eps" to get spotlight commented with "Silhouettes food" (and even better also with "Graphics" but I dont know if thats possible) without needing to make a folder action for each folder in the folder "Graphics". (Only create a folder action for the top "Graphics" folder).

What I want to accomplish with this is to be able to make a quick search for "silhouettes food apple" and hit everything right away. This way I dont have to tag each file with all the keywords.

If you don't end up with Graphics in the Spotlight Comments, it's pretty easy with Spotlight to include information in your search to help with this. Here are two different ways that should ensure only image results appear in a search. Note the punctuation.

SomeSpotlightComments kind:image
SomeSpotlightComments name:.eps

The first is looking for your desired Spotlight Comments in any image file. The second looks for your Spotlight Comments in any file whose name contains .eps. (Unfortunately, I don't believe there's a way with the Spotlight menu to specify a search using a file extension criterion. It can be done in the Find window, but I prefer not to use that for searching because it's slower than the Spotlight menu.)
NaOH is offline   Reply With Quote
Old 02-28-2012, 02:24 AM   #7
ganbustein
MVP
 
Join Date: Apr 2008
Location: Berkeley CA USA
Posts: 1,115
I'm not clear on how you're triggering your workflow. If you've saved it as a folder action, be aware that folder actions are triggered only when you add something to the relevant folder, not when you add something to a subfolder of it.

That is, suppose you've attached an Automator workflow to the folder Graphics, and the workflow contains a "Get folder contents" action and the aforementioned "Run AppleScript" action, and the Graphics folder currently contains:

Graphics
>>> Silhouettes food
>>> >>> Apple on table 1.eps

If you now drag file "Carrot in crockpot 2.eps" into "Silhouettes food", nothing will happen. You haven't added anything directly to "Graphics".

If you then drag "Silhouettes food" out of "Graphics" and back in, the folder action triggers (because now you have added an item to "Graphics"). "Carrot in crockpot 2.eps" will get a "Silhouettes food" spotlight comment, as desired. "Apple on table 1.eps", which already had that comment, will get a second copy of it. For that matter, "Silhouettes food" will get a "Graphics" comment, in addition to any it already had.

There's no easy way to get a folder action to look for additions deeper than one level down, but we can address the problem of having the same comment added over and over again, after which triggering the action manually may not be too much of a burden.

Here's an (untested) script that accepts a list of folders, and adds comments (but only as needed) recursively. Nested folders generate multiple comment terms. Existing comments are never removed. It returns a list of files whose comments were changed.

Code:
on process_folder(fldr, cmnts)
	tell application "Finder"
		repeat with itm in every folder of fldr
			process_folder(itm, cmnts & {name of fldr})
		end repeat
		
		repeat with itm in every file of fldr
			process_file(itm, cmnts)
		end repeat
	end tell
end process_folder

on process_file(fyle, cmnts)
	global output
	tell application "Finder"
		set fyle_comment to (" " & (comment of fyle) & " ")
		set changed to false
		repeat with aComment in cmnts
			if fyle_comment does not contain (" " & aComment & " ") then
				set fyle_comment to fyle_comment & aComment & " "
				set changed to true
			end if
		end repeat
		if changed then
			set comment of fyle to text 2 thru -2 of fyle_comment
			set end of output to fyle as alias
		end if
	end tell
end process_file

on run {input, parameters}
	global output
	set output to {}
	set cmnts to {}
	repeat with anItem in input
		tell application "Finder"
			if kind of anItem is folder then
				process_folder(anItem, cmnts)
			end if
		end tell
	end repeat
	return output
end run
ganbustein is offline   Reply With Quote
Old 02-28-2012, 12:42 PM   #8
frankeman90
Prospect
 
Join Date: Apr 2011
Posts: 8
Thanks a lot ganbustein and NaOH for your help on this topic.

The problem is in fact that Folder Action only looks one level and not in sub-folders. Couldnt find any solution to that even after a heavy googling seassion.

I will try your script ganbustein, maybe thats the best solution after all.
frankeman90 is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 12:50 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.