Open folder using part of a path

Is it possible to open a folder without specifying the entire path?

For eample I’ve got folders which are filed using a numeric system. My users like to add their own brief keyword after the number to help what’s in the file. I want my applescript to be able to open the folder using the six digit number and ignoring the text after that. Is there any way of using wildcard characters or some other workaround?

Currently using just number the script would be something like this:


tell application "Finder"
     if (exists folder "Target:Users:G4:Desktop:WIP applescript:100701") then
        open folder "Target:Users:G4:Desktop:WIP applescript:100701"
        activate
        else
        display dialog "this folder does not exist"
     end if
end tell

My users would like to add the keyword “Smith” to the folder name, for example, making it “Target:Users:G4:Desktop:WIP applescript:100701 Smith”. The next folder might be “Target:Users:G4:Desktop:WIP applescript:100701 Jones”.

As it stand, the applescript routine I described above would not open the renamed folders. Can anyone suggest any workarounds?

Many thanks

Model: Macbook pro
AppleScript: 2.0.1
Browser: Firefox 2.0.0.16
Operating System: Mac OS X (10.5)

You want to examine each folder that begins with the name you’re looking for. Here’s an example:

tell application "Finder"
	set parentfolder to choose folder with prompt "Choose folder:"
	set subfolders to every folder in parentfolder whose name begins with "test"
	repeat with eachfolder in subfolders
		open eachfolder
	end repeat
end tell

Thanks for your reply.

I am populating the code with information from a filemaker database. There will only ever be one folder with that particular number at the front. I have tried the code but get the following applescript error message:

Can’t get every folder of “Target:Users:G4:Desktop:EMAIL FILING:100000:10000:101000:101400:”

The populated code looks like this:


tell application "Finder"
	set parentfolder to "Target:Users:G4:Desktop:EMAIL FILING:100000:100000:101000:101400:"
	set subfolders to every folder in parentfolder whose name begins with "101490"
	repeat with eachfolder in subfolders
		open eachfolder
	end repeat
end tell

Use ‘as alias’

tell application "Finder"
    set parentfolder to "Target:Users:G4:Desktop:EMAIL FILING:100000:100000:101000:101400:" as alias
    set subfolders to every folder in parentfolder whose name begins with "101490"
    repeat with eachfolder in subfolders
        open eachfolder
    end repeat
end tell

Brilliant. That would have taken me a month of Sundays to work out! Thanks very much.

By the way, to make this a little more transportable so that it could be used in another account or system, you could do something like this:

tell application "Finder"
	set parentfolder to path to desktop folder as text
	set parentfolder to parentfolder & "EMAIL FILING:100000:100000:101000:101400:" as alias
end tell

As long as there is a folder on the desktop called ‘EMAIL FILING’ that contains the above subfolders, the script will work.

When I run the following script:


tell application "Finder"
	--gets the path of the folder to work with
	set parentfolder to "Target:Users:G4:Desktop:WIP applescript" as alias
	set subfolders to every folder in parentfolder whose name begins with "1007"
	--makes sure the folder exists
	set folderqty to count of subfolders
	if folderqty < 0 then
		display dialog "This folder does not exist" buttons {"OK"}
	else
		--saves up-to-date name
		set foldername to item 1 of subfolders
		set name of folder foldername to "1007 smith"
	end if
end tell

I get the following dialog:
Finder got an error: Can’t make folder “1007 smith” of folder “WIP applescript” of folder “Desktop” of startup disk into type integer.
Unknown Error: -1700

Do you know what I am missing?

:wink:
Use ‘as alias’

tell application "Finder"
    --gets the path of the folder to work with
    set parentfolder to "Target:Users:G4:Desktop:WIP applescript" as alias
    set subfolders to every folder in parentfolder whose name begins with "1007"
    --makes sure the folder exists
    set folderqty to count of subfolders
    if folderqty < 0 then
        display dialog "This folder does not exist" buttons {"OK"}
    else
        --saves up-to-date name
        set foldername to item 1 of subfolders as alias
        set name of folder foldername to "1007 smith"
    end if
end tell

Hi waggy,

Your script code contains just one word too much :wink: The variable foldername is already a full reference to a folder object, so you do not need to address is as a folder in your script:


set name of folder foldername to "1007 smith"

vs.


set name of foldername to "1007 smith"

A working version of your code:


tell application "Finder"
	--gets the path of the folder to work with
	set parentfolder to "Target:Users:G4:Desktop:WIP applescript" as alias
	set subfolders to every folder in parentfolder whose name begins with "1007"
	--makes sure the folder exists
	set folderqty to count of subfolders
	if folderqty < 0 then
		display dialog "This folder does not exist" buttons {"OK"}
	else
		--saves up-to-date name
		set foldername to item 1 of subfolders
		set name of foldername to "1007 smith"
	end if
end tell

I was trying “as text” and “as alias” everywhere but the right place.

It’s such a thrill when the damn thing actually works! Thank you.

Martin, thankyou also for your comments. I have included them in my script.

simple version ;):


try
	tell application "Finder" to set name of (1st folder of folder "WIP applescript:" whose name is "1007") to "1007 smith"
on error e number n
	if n = -10006 then
		set errText to "A folder '1007' does not exist"
	else if n = -48 then
		set errText to "The folder '1007 smith' already exists"
	else
		set e to "error " & e & " (" & n & ") occured"
	end if
	tell application "Finder" to display dialog errText buttons {"OK"}
end try

Stefan. That’s a really neat (as in well-crafted) solution. I have introduced the first bit and am just working out how to use the error checking dialogs. Many thanks to everybody for their time on this.