Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Help cleaning up a script to search in Finder with Spotlight?

I don't know much AppleScript at all, but managed to cobble together a script (invoked with Alfred) that opens a Finder window, sets the size, view options etc. and searches for Photoshop documents with the term passed from Alfred.


As it's cobbled together I'm guessing there are much cleaner, efficient, and more elegant ways of doing this. Any help cleaning it up would be greatly appreciated. Here's the script:



-- set q to "wobbly jelly" -- Test search


on alfred_script(q)


set theSearch to q


tell application "Finder"


activate


opencomputer container



-- Set the folder view

tell Finder window 1

set the bounds to {630, 150, (630 + 1300), (150 + 1100)}

set toolbar visible to true

set the sidebar width to 180

end tell

end tell


tell application "System Events"


-- Focus to the search field


keystroke "f" using {command down, option down}

delay 0.4



-- Type the search

keystroke "kind:photoshop " & theSearch

end tell



-- Arrange by…

menu_click({"Finder", "View", "Arrange By", "Date Modified"})



-- Set to Icon View

tell application "Finder"

set current view of Finder window 1 to icon view


tell icon view options of Finder window 1

set icon size to 128

set shows icon preview to true

set shows item info to true

end tell

end tell


end alfred_script


-- `menu_click`, by Jacob Rus, September 2006

-- Accepts a list of form: `{"Finder", "View", "Arrange By", "Date"}`

-- Execute the specified menu item. In this case, assuming the Finder

-- is the active application, arranging the frontmost folder by date.

on menu_click(mList)

local appName, topMenu, r



-- Validate our input

if mList's length < 3 then error "Menu list is not long enough"



-- Set these variables for clarity and brevity later on

set {appName, topMenu} to (items 1 through 2 of mList)

set r to (items 3 through (mList's length) of mList)



-- This overly-long line calls the menu_recurse function with


-- two arguments: r, and a reference to the top-level menu

tell application "System Events" to my menu_click_recurse(r, ((process appName)'s ¬

(menu bar 1)'s (menu bar itemtopMenu)'s (menutopMenu)))

end menu_click


on menu_click_recurse(mList, parentObject)

local f, r



-- `f` = first item, `r` = rest of items

set f to item 1 of mList

if mList's length > 1 then set r to (items 2 through (mList's length) of mList)



-- either actually click the menu item, or recurse again

tell application "System Events"

if mList's length is 1 then


clickparentObject'smenu itemf

else

my menu_click_recurse(r, (parentObject's (menu itemf)'s (menuf)))

end if

end tell

end menu_click_recurse

Posted on Jan 29, 2014 2:05 PM

Reply
Question marked as Best reply

Posted on Jan 29, 2014 5:49 PM

Mainly just grouping the separate cobbles together, although there is a shortcut that gets rid of all that System Events stuff for the find parameter. Note that in Mavericks the icon view won't immediately update - changes will be applied when the window target changes.

on alfred_script(theSearch)   tell application "Finder"     «event aevtspot» "kind:photoshop " & theSearch -- shhh - don't tell anyone...     tell Finder window 1 -- Set the folder view       set the bounds to {630, 150, (630 + 1300), (150 + 1100)}       set toolbar visible to true       set the sidebar width to 180       set current view to icon view -- set the icon view       tell its icon view options -- view options may not immediately update in Mavericks         set icon size to 128         set shows icon preview to true         set shows item info to true       end tell     end tell   end tell   menu_click({"Finder", "View", "Arrange By", "Date Modified"}) -- Arrange by... end alfred_script

7 replies
Question marked as Best reply

Jan 29, 2014 5:49 PM in response to JonoH

Mainly just grouping the separate cobbles together, although there is a shortcut that gets rid of all that System Events stuff for the find parameter. Note that in Mavericks the icon view won't immediately update - changes will be applied when the window target changes.

on alfred_script(theSearch)   tell application "Finder"     «event aevtspot» "kind:photoshop " & theSearch -- shhh - don't tell anyone...     tell Finder window 1 -- Set the folder view       set the bounds to {630, 150, (630 + 1300), (150 + 1100)}       set toolbar visible to true       set the sidebar width to 180       set current view to icon view -- set the icon view       tell its icon view options -- view options may not immediately update in Mavericks         set icon size to 128         set shows icon preview to true         set shows item info to true       end tell     end tell   end tell   menu_click({"Finder", "View", "Arrange By", "Date Modified"}) -- Arrange by... end alfred_script

Feb 1, 2014 11:45 AM in response to red_menace


Thanks for clarifying that.


Would it be possible for example to open the documents folders and then make it search just that folder with «event aevtspot»?

I could open the window, focus the search field and make it type the search (like I did previously), but would prefer the newer more elegant way if possible 🙂

Help cleaning up a script to search in Finder with Spotlight?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.