Search for Folder in a Folder based on criteria?

Hello,
Very new to Applescript. Automator is more in my wheelhouse, but I’m learning.

I’m trying to write a script that will search a specific folder for another folder within it based on certain criteria. Basically I know for sure that part of the Folder Name will have specific text in it and want to target that and return the selected folder. I found a script here on Macscripter that is exactly what I am trying to achieve, but I am getting consistent errors.

set theFolder to alias "~Desktop:testing"

tell application "Finder"
	items of theFolder whose name contains "~action_items"
end tell

This is the error I’m getting.
error “File alias ~Desktop:testing of «script» wasn’t found.” number -43

Ideally, I’d like to include an IF in the script so that if it is unable to find the folder within the folder, it will display a dialog and quit the application. (this will be running from an app)

would anyone like to take pity on me and try to fill in the blanks?

Hi miabifilms,

To see how an alias reference looks, run this and see the result.


set desktop_path to (path to desktop)

‘desktop’ is a reserved word for the desktop. concatenate the name of your folder located on the desktop.


set desktop_path to (path to desktop)
set my_folder_ref to ((desktop_path as string) & "testing:") as alias

Editted: btw, another easy way to see how references look is to use ‘choose folder’ or ‘choose file’.


set my_folder_ref to choose folder

result: alias “Macintosh HD:Users:kelhome:Desktop:testing:”

gl,

thanks kel1, No error that time.

Taking it one step further, I’m trying to set up my If statement now… I can’t figure out what’s going wrong. without the IF, this works, but with it, it’s not returning the result of the search.

set theFolder to alias "Macintosh HD:Users:grato:Desktop:testing:"

tell application "Finder"
	items of theFolder whose name contains "-action_items"
	set actionItems to result
	if actionItems exists then
		return actionItems
	else
		display dialog with title "Looks like there are no action items." buttons {"Ok"}
		
	end if
end tell

HI,

I didn’t test this, but it looks ok:


set theFolder to alias "Macintosh HD:Users:grato:Desktop:testing:"

tell application "Finder"
	items of theFolder whose name contains "-action_items"
	set actionItems to result -- list of items if any
	if actionItems is not {} then -- empty list
		return actionItems
	else
		display dialog with title "Looks like there are no action items." buttons {"Ok"}
		
	end if
end tell

Note that there are three reference forms: alias, file, and Finder references. The Finder references look different like:

folder “some folder” of folder “another folder” … of startup disk of application “Finder”

Sometimes, you can’t use Finder references like this outside the “Finder” or “System Events” tell block I think. Most of the time you can though.

gl,

Model: MBP
AppleScript: 2.2.3
Browser: Safari 536.26.17
Operating System: Mac OS X (10.8)

Thanks for holding my hand through this Kel1. It helped a lot!