Re: do shell scripting a "Find"
Re: do shell scripting a "Find"
- Subject: Re: do shell scripting a "Find"
- From: kai <email@hidden>
- Date: Mon, 28 Feb 2005 02:44:06 +0000
On Fri, 25 Feb 2005 17:29:34 -0000, "M Ranes" wrote:
I am wanting to find files and applications using shell & applescript.
Nothing is working, is there anyone who can show me a working method,
post a snippet to start from, or just some tidbit to work with. I am
trying to FIND an application or file whose name contains "TheName"
and return a path, completely in the background? Please help.
I see you've had a few replies that look as though they should help,
Marc. Since there's not much yet in terms of vanilla AS, I guess it
might be worth throwing one or two of them into the pot.
To avoid embarking on a volume-wide search, it obviously helps if you
have some idea of where to start looking for the application file in
question. I've generally assumed that such a search might logically
involve the applications folder. The search methods below mainly
involve the Finder. (After pasting into a script editor, there are
bound to be some line wraps to fix.)
The following snippet simply looks for the first file in the
applications folder to satisfy the specified condition:
--------------------
set theName to "text"
set searchFolder to path to "apps" from System domain
tell application "Finder" to set appPath to (searchFolder's first
application file whose name contains theName) as Unicode text
--> "Macintosh HD:Applications:TextEdit.app"
--------------------
If you're looking for more than one file that matches certain criteria,
something like this might do the trick:
--------------------
to getpathList from f against t
set text item delimiters to return
tell application "Finder" to set l to ((folder f's application files
whose name contains t) as Unicode text)'s paragraphs
set text item delimiters to {""}
l
end getpathList
set theName to "net"
set searchFolder to path to "apps" from System domain
set pathList to getpathList from searchFolder against theName
--> {"Macintosh HD:Applications:Internet Connect.app", "Macintosh
HD:Applications:Internet Explorer.app"}
--------------------
However, the problem with the above methods is that they search the
targeted folder at only one level. There are a few ways to explore
subfolders as well - one of them being the Finder's 'entire contents'
container property:
--------------------
to getpathList from f against t
set text item delimiters to return
tell application "Finder" to set l to ((application files of entire
contents of f whose name contains t) as Unicode text)'s paragraphs
set text item delimiters to {""}
l
end getpathList
set theName to "net"
set searchFolder to path to "apps" from System domain
getpathList from searchFolder against theName
--> {"Macintosh HD:Applications:Internet Connect.app", "Macintosh
HD:Applications:Internet Explorer.app", "Macintosh
HD:Applications:Utilities:NetInfo Manager.app", "Macintosh
HD:Applications:Utilities:Network Utility.app"}
--------------------
This approach can be quite slow - especially when files are numerous
and deeply nested. It's sometimes faster to use a repeat loop or
recursive handler, such as the 'getpathList' subroutine below - which
calls itself to tunnel through subfolders. OMM, it's about 75% faster
than the above suggestion:
--------------------
to getpathList from f against t
tell application "Finder"
set l to (f's application files whose name contains t) as Unicode text
repeat with i in (get f's folders)
set l to l & my (getpathList from i against t)
end repeat
end tell
l's paragraphs
end getpathList
set theName to "net"
set searchFolder to path to "apps" from System domain
set text item delimiters to return
set pathList to getpathList from searchFolder against theName
set text item delimiters to {""}
pathList
--> {"Macintosh HD:Applications:Internet Connect.app", "Macintosh
HD:Applications:Internet Explorer.appMacintosh
HD:Applications:Utilities:NetInfo Manager.app", "Macintosh
HD:Applications:Utilities:Network Utility.app"}
--------------------
Finally, I hope you'll forgive me if I go off at a speculative tangent.
You say that you're looking for a way to match partial application file
names, and I'm wondering about the context of this. For example, you
may want to find a particular application with different version
numbers included in the filename (Photoshop, AppleWorks, etc.). If
that's the case, then you might consider using the Finder's application
file id, which is pretty fast. The syntax might go something like these
examples:
--------------------
tell application "Finder"
application file id "prvw"
end tell
--> application file "Preview.app" of folder "Applications" of startup
disk of application "Finder"
--------------------
tell application "Finder" to set appPath to application file id "trmx"
as Unicode text
--> "Macintosh HD:Applications:Utilities:Terminal.app"
--------------------
tell application "Finder" to set appAlias to application file id
"wtxt" as alias
--> alias "Macintosh HD:Developer:Applications:Extras:WorldText.app:"
--------------------
Note that it's not necessary to specify a search folder - so this
method is good wherever the application file might be hidden.
To determine the relevant id code, use something like one of the
following:
--------------------
set f to choose file of type {"APPL"}
tell application "Finder" to f's creator type
--------------------
file creator of (info for (choose file of type {"APPL"}))
--------------------
While there may be some exceptions (usually returning "????" - which
won't work), most application files should have an id. Here are just a
few examples:
--------------------
"MeSa" = Adobe ImageReady
"8BIM" = Adobe Photoshop
"CARO" = Adobe Reader
"1wcu" = AirPort Admin Utility
"hiti" = Apple Help Indexing Tool
"BOBO" = AppleWorks
"auDy" = Audacity
"AMDS" = Audio MIDI Setup
"bomb" = BombApp
"ctcp" = Chinese Text Converter
"bxls" = Classic Startup
"csut" = ColorSync Utility
"cmtr" = DigitalColor Meter
"ddsk" = Disk Copy
"CSca" = Display Calibrator
"dock" = Dock
"DStf" = DropStuff
"crsT" = DropTar
"DZip" = DropZip
"cfbs" = DVD Player
"MACS" = Finder
"hbwr" = Help Viewer
"wrbt" = iCal
"fez!" = iChat
"Mngl" = Iconographer
"TBPI" = IM Plugin Converter
"icac" = Image Capture
"Hway" = iMovie
"xins" = Installer
"nib!" = Interface Builder
"netc" = Internet Connect
"MSIE" = Internet Explorer
"iPho" = iPhoto
"dopi" = iPod Updater
"iste" = iSight Updater
"hook" = iTunes
"jnlp" = Java Web Start
"jbrs" = JavaBrowser
"KrbM" = Kerberos
"keyc" = Key Caps
"kcmr" = Keychain Access
"lgnw" = loginwindow
"emal" = Mail
"OPIM" = Microsoft Entourage
"XCEL" = Microsoft Excel
"PPT3" = Microsoft PowerPoint
"MSWD" = Microsoft Word
"MSNS" = MSN Messenger
"NetU" = Network Utility
"odbc" = ODBC Administrator
"oglp" = OpenGL Profiler
"ogls" = OpenGL Shader Builder
"pmkr" = PackageMaker
"PfED" = PEFViewer
"SNPL" = PlayerPRO
"prvw" = Preview
"pctr" = Print Center
"pvwr" = Process Viewer
"pbxa" = Project Builder
"pled" = Property List Editor
"TVOD" = QuickTime Player
"PNst" = RealOne Player
"RsFl" = ResFool
"sfri" = Safari
"ToyS" = Script Editor
"MBX " = Setup Assistant
"sktc" = Sketch
"VIZF" = Smile
"swup" = Software Update
"notz" = Stickies
"SITx" = StuffIt Expander
"sevs" = System Events
"sprf" = System Preferences
"syui" = SystemUIServer
"trmx" = Terminal
"TBB6" = Tex-Edit Plus
"ttxt" = TextEdit
"thvw" = Thread Viewer
"Gaji" = TrueType Font Editor
"USBP" = USB Prober
"Ms01" = Windows Media Player
"wTap" = WireTap
"wtxt" = WorldText
--------------------
If more than one version of a particular application is present, the
application file returned will normally be the latest version.
For example, if I run the following script OMM, it reveals several
different versions of iPod updater...
--------------------
set f to (path to "utiĆ’" as Unicode text) & "iPod Software
Updater.localized" as alias
tell application "Finder" to name of f's application files whose
creator type is "dopi"
--> {"iPod Updater 2004-04-28.app", "iPod Updater 2004-08-06.app",
"iPod Updater 2004-11-15.app", "iPod Updater 2005-02-22.app"}
--------------------
...while this returns only the latest updater:
--------------------
tell application "Finder" to application file id "dopi" as alias
--> alias "Macintosh HD:Applications:Utilities:iPod Software
Updater.localized:iPod Updater 2005-02-22.app:"
--------------------
Of course, all this may have very little to do with what you're trying
to achieve at the moment - in which case I hope you'll find it useful
at some other time... :-)
---
kai
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden