Script which handles very long file paths

I am looking for a script which allows me to recursively check a folder for files that have a certain name. I then want the script to send an open message to the Finder so that all files with that name are opened.

I didn’t think this should be very difficult. I found the script “unlock_files_X.1.2” on this site, but the problem with that script seems to be that it cannot handle very long path names. The files I am looking for can be as much as 30 folders deep and each folder name can be very long. Does anyone have any ideas? Thanks!

Not possible then? :frowning:

Some of these should help you

set x to alias "path:to:some:folder:"
set longString to "lalafutis asdfa sdf asdf as fsa fasd fasd fasdf asdf as fas fas fdasdf asdf asdf asdfasdf asdfasd fasd fasd fasd fasd asd asdf asd ff asda sfds dsdf"
paragraphs of (do shell script "find " & quoted form of POSIX path of x & " -name " & quoted form of longString & "*")
set x to alias "path:to:some:folder:"
set longString to "lalafutis asdfa sdf asdf as fsa fasd fasd fasdf asdf as fas fas fdasdf asdf asdf asdfasdf asdfasd fasd fasd fasd fasd asd asdf asd ff asda sfds dsdf"
tell application "Finder" to every file of entire contents of x whose name contains longString

You can also pick some of these routines and adaptate it to fit your needs…

http://macscripter.net/exchange/comments.php?id=173_0_1_0_C
http://macscripter.net/exchange/comments.php?id=174_0_1_0_C

:wink:

Thanks, I now have something that works. Another problem though:

Some of the files I’m looking for are actually inside package folders which are treated as file by AppleScript.

Let’s say that I want to open all “locversion.plist” files in a certain folder. If you look inside almost any Apple application you will see this file. I have no problem opening these files if they’re not inside package folder, but how should I go about opening these files as well? The script I have so far is below. It opens all “locversion.plist” files, except those in package folder.

Thanks so much for any help you might be able to give me!

global myFileList, outFolder

on run
set myFileList to {}
set theFolder to choose folder with prompt “Please select a folder…”
tell application “Finder”
activate
end tell
parseFolder(theFolder) of me
end run

on parseFolder(aFolder)
tell application “Finder”
set ListFolders to every folder of aFolder
repeat with ItemFolder in ListFolders
set end of myFileList to (every file of ItemFolder whose name contains “locversion.plist”)
if (count folders in ItemFolder) > 0 then parseFolder(ItemFolder) of me
end repeat
open every item of myFileList
set y to number of items in myFileList
set myFileList to {}
end tell
end parseFolder

list folder command will show files within packages.

Also, you can use shell’s “find”:

do shell script "find " & quoted form of posix path of alias "path:to:bundle.app:" & " | grep -e locversion.plist"

Which will return a list of any file matching “locversion.plist” within the related folder…
A second example, to find any “locversion.plist” whitin your applications folder and nesteed folders:

do shell script "find /applications | grep -e locversion.plist"

Wow, that’s really powerful! Since I’m a bit of a newbie though, I have some trouble putting this type of command successfully in my script. My script now looks like this, but I get an error when it gets executed. The solution is probably quite simple, but what is it??

global myFileList, outFolder

on run
set myFileList to {}
set theFolder to choose folder with prompt “Please select a folder…”
tell application “Finder”
activate
end tell
parseFolder(theFolder) of me
end run

on parseFolder(aFolder)
do shell script “find " & quoted form of POSIX path of alias aFolder & " | grep -e locversion.plist”
set myFileList to the result
tell application “Finder”
open every item of myFileList
end tell
end parseFolder

The “do shell script” stuff will return a semi-list of files, which is not really a list, but a text:

"/disk/user/name/file
/disk/user/name/folder/file
/disk/user/name/folder/subfolder/file"

So, to tell the Finder to open such files, you must coerce each one to type alias. Eg:

tell app "Finder" to open ("/disk/user/name/file" as posix file as alias)

But, since you like the shell, you can also try this:

set myFileList to myFileList's paragraphs
set AppleScript's text item delimiters to "';open '"
set myFileList to "open '" & myFileList & "'"
set AppleScript's text item delimiters to {""}
myFileList
--> "open '/disk/user/name/file';open '/disk/user/name/folder/file';open '/disk/user/name/folder/subfolder/file'"
do shell script myFileList

Thanks again! Unfortunately, I’m still not there, because I cannot try your last suggestion. The reason is that my latest script as I posted it does not work:


on parseFolder(aFolder)
do shell script "find " & quoted form of POSIX path of alias aFolder & " | grep -e locversion.plist" 
set myFileList to the result 
tell application "Finder" 
open every item of myFileList 
end tell 
end parseFolder

When I run the script, I get this error:

Can’t make quoted form of POSIX path of alias (alias "Jaguar:Applications:Utilities:) of <> into a string

So I don’t even get to try your last suggestion. I guess I’m almost there, but not quite.

Oh! I see the point. aFolder is already an alias, so you fail here:

Should be:

do shell script "find " & quoted form of POSIX path of aFolder & " | grep -e locversion.plist"

Hey, that’s funny! I tried this on another machine, and I got a “type 1 error”. I’ve now tried it on yet another machine, and it works like a dream!!

Thanks for all the answers! You’ve been a great help!

One last thing (hopefully)…

I edited my script to include a repeat loop using the variable x:


	set myFileList to "open '" & myFileList & "'"
	set x to the number of items of myFileList

When “myFileList” contains a lot of files (well, perhaps not even that many, a couple of hundred or so), the script quits with a stack overflow error. With the script open, the word “number” in the 2nd line is highlighted. Why would that be? The variable isn’t that big, or is it?

Anyway, may be there’s a cleverer way of getting the result that I want - I only want to open files called “locversion.plist” in “English.lproj” folder. I’m not interested in other language folders. May be this can be achieved by modifying the grep command?

Yep! You receive a stack overflow error when so many data (usually in a list)
You can use grep:

do shell script "find /applications/utilities | grep -e English.lproj/locversion.plist"

As easy as that… I’ll get myself a good AppleScript book and learn more AppleScript. It’s wonderful what you can do with it!

Thanks again!

One more thing to make the script complete.

How do I modify the “do shell script” line…

do shell script “find /applications/utilities | grep -e English.lproj/locversion.plist”

…so that I only get a list of all “English.lproj” folders?

When I try this:

do shell script “find /applications/utilities | grep -e English.lproj”

I get a list of all English.lproj folders and everything inside it. I would like to have a list of just all English.lproj folders. I suspect that I just need a very small change to the statement above, but I don’t know what it should be. I’ve tried to search around, but didn’t find the solution. Any help is much appreciated!

Here!

find /Applications/Utilities | grep -e 'English.lproj$'

The $ sign means “ends with”… I’m just discovering regular expressions, and they are very interesting :!: :smiley:

Works great! Thanks!

An alternative, which wasn’t going to be an alernative when I logged in:


find /Applications/Utilities -name English.lproj

Should actually be faster because find doesn’t have to create a very large list and pipe it to grep, it is actually filters the names for itself.

Also you wont find this unix command line stuff in an apple script book. So you’ll need an AppleScript book and a book like unix for dummies or learning Unix for Mac OS X, which is pretty basic.

An AppleScript book that I would recommend is Ethan Wilde’s AppleScript for applications.

Just for showing off, if you want to find all the locversion.plist files that contain the text “fr” then this does the trick.


find /Applications/Utilities -name locversion.plist -exec grep -H "fr" {} ;