Get names and versions of application files

Hi,

I’ve been using this:

tell application "Finder"
			with timeout of 1 * minutes seconds
				set the_list to name of every application file in entire contents of (path to applications folder)
			end timeout
		end tell

to list apps in my apps folder. I’d like to append their short versions to the end of their items in the_list, what’s the neatest way to do that?

Thanks,

Joe

Hi Joe,

is this sufficient?


do shell script "system_profiler SPApplicationsDataType"

Hello StephanK

Your script works too much.

It returns applications from the Developer’s folder which is not in the Applications’s one :wink:
Here, it ends with
Xcode:

  Version: 2.0
  Last Modified: 08/04/2008 13:23
  Kind: PowerPC
  Get Info String: Xcode v2.0
  Location: /Developer/Applications/Xcode.app

Yvan KOENIG (from FRANCE mardi 13 mai 2008 18:59:42)

d’accord monsieur :slight_smile:
What’s about this one:


set the_list to {}
set theApplications to paragraphs of (do shell script "system_profiler SPApplicationsDataType | grep 'Version\\|Location'")
set TID to text item delimiters
set idx to 1
set maxApps to count theApplications
repeat until idx is maxApps
	if item idx of theApplications contains "Version: " and item (idx + 1) of theApplications contains "Location: /Applications" and item (idx + 1) of theApplications contains ".app" then
		set text item delimiters to "Version: "
		set theVersion to text item 2 of item idx of theApplications
		set text item delimiters to "/"
		set end of the_list to text 1 thru -5 of last text item of item (idx + 1) of theApplications & space & theVersion
		set idx to idx + 2
	else if item idx of theApplications contains "Location: /Applications" and item idx of theApplications contains ".app" then
		set text item delimiters to "/"
		set end of the_list to text 1 thru -5 of last text item of item idx of theApplications
		set idx to idx + 1
	else
		set idx to idx + 1
	end if
end repeat
set text item delimiters to TID
the_list

Try something like this:

set someFolder to path to applications folder

do shell script "/usr/bin/mdls -name kMDItemDisplayName -name kMDItemVersion " & quoted form of POSIX path of someFolder & "*.app | /usr/bin/colrm 1 22 | /usr/bin/ruby -e 'items = STDIN.readlines; items.each_index {|i| items[i].chomp!().chomp!(\"\\\"\"); print((i.modulo(2) == 0 ? items[i] : (items[i] != \"null)\") ? \" \"+items[i]+\"\\n\" : \"\\n\")) }'"
set appList to paragraphs of result

Note that mdls requires Mac OS X 10.4 or later.

This is really amazing (and pretty fast) :smiley:

Stephan, thanks for your help, but that was the point I started at (scripting system profiler data types) and needed to get ‘back to basics’ as much as possible. Thanks for your help though in producing that second script which presumably set Bruce going with his fantastic contribution!

I would never have come up with that myself, thanks Bruce (and everybody) :smiley:

My I just ask though, is there a way to make this work to a recursive depth of the next folder? This is so that the script could pick up applications in subfolders (like adobe apps for example)

Try something like this:

set someFolder to path to applications folder

do shell script "/usr/bin/mdfind -0 -onlyin " & ¬
	quoted form of POSIX path of someFolder & ¬
	" \"kMDItemContentTypeTree == 'com.apple.application*'\"" & ¬
	" | /usr/bin/xargs -0 " & ¬
	" /usr/bin/mdls -name kMDItemDisplayName -name kMDItemVersion" & ¬
	" | /usr/bin/colrm 1 22" & ¬
	" | /usr/bin/ruby -e " & ¬
	"'items = STDIN.readlines; items.each_index {|i| items[i].chomp!().chomp!(\"\\\"\"); print((i.modulo(2) == 0 ? items[i] : (items[i] != \"null)\") ? \" \"+items[i]+\"\\n\" : \"\\n\")) }'" & ¬
	" | /usr/bin/sort --ignore-case"
set appList to paragraphs of result

Hello

I’m running 10.4.11 in French

When I run your script, I get this report:

tell current application
path to applications folder
alias “Caviar Macintosh HD:Applications:”
do shell script “/usr/bin/mdfind -0 -onlyin ‘/Applications/’ "kMDItemContentTypeTree == ‘com.apple.application*’" | /usr/bin/xargs -0 /usr/bin/mdls -name kMDItemDisplayName -name kMDItemVersion | /usr/bin/colrm 1 22 | /usr/bin/ruby -e ‘items = STDIN.readlines; items.each_index {|i| items[i].chomp!().chomp!("\""); print((i.modulo(2) == 0 ? items[i] : (items[i] != "null)") ? " "+items[i]+"\n" : "\n")) }’ | /usr/bin/sort --ignore-case”
“/usr/bin/sort: unrecognized option --' Try /usr/bin/sort --help’ for more information.
-e:1:in write': Broken pipe (Errno::EPIPE) from -e:1:in print’
from -e:1
from -e:1:in `each_index’
from -e:1”

I don’t know ruby so I am unable to get rid of that.

When I run your first script, I get this report which display an odd list.

tell current application
path to applications folder
alias “Caviar Macintosh HD:Applications:”
do shell script “/usr/bin/mdls -name kMDItemDisplayName -name kMDItemVersion ‘/Applications/’*.app | /usr/bin/colrm 1 22 | /usr/bin/ruby -e ‘items = STDIN.readlines; items.each_index {|i| items[i].chomp!().chomp!("\""); print((i.modulo(2) == 0 ? items[i] : (items[i] != "null)") ? " "+items[i]+"\n" : "\n")) }’”
“Book.app ------------- Carnet d’adresses
4.0.6 or.app -------------
Calculette 4.0.6
app ------------- ClamXav
verter.app ------------- DivX Converter
1.2 yer.app -------------
DivX Player 2.2
ites.app ------------- Extra Suites
1.1 nd.app -------------
GarageBand 4.1.2
.app ------------- GeoGebra
pture.app ------------- Transfert d’images
3.0 Connect.app -------------
Connexion à Internet 1.4.3
------------- Mail
2.1 ick.app -------------
MemoryStick 1.2.1
e.app ------------- NeoOffice
2.2.3 ce.org 2.2.app -------------
OpenOffice.org 2.2.app ce.org 2.3.app -------------
OpenOffice.org 2.3.app 2.3.0
ce.org 2.4.app ------------- OpenOffice.org 2.4.app
2.4.0 app -------------
Aperçu 3.0.9
e Player.app ------------- QuickTime Player
7.4.5 sizer.app -------------
RapidoResizer 1.0.8
ite.app ------------- RapidoWrite
pp ------------- Safari
3.0.4 .app -------------
Sherlock 3.6.1
hoenix.app ------------- StellarPhoenix
.app ------------- Aide-mémoire
5.1.0 references.app -------------
Préférences Système 3.3
.app ------------- TextEdit
1.4 -------------
iCal 2.0.5
p ------------- iChat
3.1.8 -------------
iDVD 7.0.1
pp ------------- iMovie
7.1.1 pp -------------
iPhoto 7.1.3
p ------------- iSync
2.4 pp -------------
iTunes 7.6.2
------------- iWeb
2.0.3”
end tell

Yvan KOENIG

I’m on 10.5. What do you get when you run this:

do shell script "/usr/bin/mdls -name kMDItemDisplayName -name kMDItemVersion /Applications/i*.app"

Bruce, your post #8 is fantastic, just perfect.

Thanks,

Joe

Hello Bruce

On 10.4.11 I get:

tell current application
do shell script “/usr/bin/mdls -name kMDItemDisplayName -name kMDItemVersion /Applications/i*.app”
“/Applications/iCal.app -------------
kMDItemDisplayName = "iCal"
kMDItemVersion = "2.0.5"
/Applications/iChat.app -------------
kMDItemDisplayName = "iChat"
kMDItemVersion = "3.1.8"
/Applications/iDVD.app -------------
kMDItemDisplayName = "iDVD"
kMDItemVersion = "7.0.1"
/Applications/iMovie.app -------------
kMDItemDisplayName = "iMovie"
kMDItemVersion = "7.1.1"
/Applications/iPhoto.app -------------
kMDItemDisplayName = "iPhoto"
kMDItemVersion = "7.1.3"
/Applications/iSync.app -------------
kMDItemDisplayName = "iSync"
kMDItemVersion = "2.4"
/Applications/iTunes.app -------------
kMDItemDisplayName = "iTunes"
kMDItemVersion = "7.6.2"
/Applications/iWeb.app -------------
kMDItemDisplayName = "iWeb"
kMDItemVersion = "2.0.3"”
end tell

Don’t worry, I was just trying to understand the beast.

Yvan KOENIG (from FRANCE mercredi 14 mai 2008 18:01:20)

tell current application
path to applications folder
alias “Caviar Macintosh HD:Applications:”
do shell script “/usr/bin/mdfind -0 -onlyin ‘/Applications/’ "kMDItemContentTypeTree == ‘com.apple.application*’" | /usr/bin/xargs -0 /usr/bin/mdls -name kMDItemDisplayName -name kMDItemVersion”
"/Applications/Utilities/Disk Utility.app -------------
kMDItemDisplayName = "Utilitaire de disque"
kMDItemVersion = "10.5.6"
/Applications/Utilities/Installer.app -------------
kMDItemDisplayName = "Programme d’installation"
kMDItemVersion = "2.1.9"
/Applications/Utilities/Network Utility.app -------------
kMDItemDisplayName = "Utilitaire de réseau"
kMDItemVersion = "1.4.2"
/Applications/Utilities/System Profiler.app -------------
kMDItemDisplayName = "Informations Système"
kMDItemVersion = "4.1.1"
/Applications/Utilities/Terminal.app -------------
kMDItemDisplayName = "Terminal"
kMDItemVersion = "1.5.1"
/Applications/Utilities/VoiceOver Utility.app -------------
kMDItemDisplayName = "VoiceOver Utility"
kMDItemVersion = "1.0"
/Applications/AppleWorks 6/Généralités AppleWorks/Scripts/Ostrea/collervaleursseules.app -------------
kMDItemDisplayName = "collervaleursseules"
/Applications/AppleWorks 6/Généralités AppleWorks/Scripts/Ostrea/encadrertotaux2.app -------------
kMDItemDisplayName = "encadrertotaux2"
/Applications/AppleWorks 6/Généralités AppleWorks/Scripts/Ostrea/indices.app -------------
kMDItemDisplayName = "indices"
/Applications/AppleWorks 6/Généralités AppleWorks/Scripts/Ostrea/largeur.app -------------
kMDItemDisplayName = "largeur"
/Applications/AppleWorks 6/Généralités AppleWorks/Scripts/Ostrea/largeurs.app -------------
kMDItemDisplayName = "largeurs"
/Applications/AppleWorks 6/Généralités AppleWorks/Scripts/Ostrea/saut.app -------------
kMDItemDisplayName = "saut"
/Applications/AppleWorks 6/Généralités AppleWorks/Scripts/Ostrea/souligner.app -------------
kMDItemDisplayName = "souligner"
/Applications/AppleWorks 6/Généralités AppleWorks/Scripts/Ostrea/tri.app -------------
kMDItemDisplayName = "tri"
/Applications/AppleWorks 6/Généralités AppleWorks/Scripts/relevé 4 peggy (v5).app -------------
kMDItemDisplayName = "relevé 4 peggy (v5)"
/Applications/AppleWorks 6/Généralités AppleWorks/Scripts/ƒ tableur ƒ/SS date fixe 4.app -------------
kMDItemDisplayName = "SS date fixe 4"
/Applications/AppleWorks 6/Généralités AppleWorks/Scripts/ƒboutonsƒ/AW colle_centre SS.app -------------
kMDItemDisplayName = "AW colle_centre SS"
/Applications/AppleWorks 6/Généralités AppleWorks/Scripts/ƒboutonsƒ/AW Helvetica 12.app -------------
kMDItemDisplayName = "AW Helvetica 12"
/Applications/AppleWorks 6/Généralités AppleWorks/Scripts/ƒboutonsƒ/AW Helvetica 9.app -------------
kMDItemDisplayName = "AW Helvetica 9"
/Applications/AppleWorks 6/Généralités AppleWorks/Scripts/ƒboutonsƒ/AW marginsFontSize.app -------------
kMDItemDisplayName = "AW marginsFontSize"
/Applications/AppleWorks 6/Généralités AppleWorks/Scripts/ƒboutonsƒ/AW save as 5.app -------------
kMDItemDisplayName = "AW save as 5"
/Applications/AppleWorks 6/Généralités AppleWorks/Scripts/ƒboutonsƒ/AW texte en rouge.app -------------
kMDItemDisplayName = "AW texte en rouge"
/Applications/AppleWorks 6/Généralités AppleWorks/Scripts/ƒboutonsƒ/AW6 collerMieux_2.app -------------
kMDItemDisplayName = "AW6 collerMieux_2"
/Applications/AppleWorks 6/Généralités AppleWorks/Scripts/ƒboutonsƒ/AW6 copier1peuMieux_2.app -------------
kMDItemDisplayName = "AW6 copier1peuMieux_2"
/Applications/AppleWorks 6/Généralités AppleWorks/Scripts/ƒboutonsƒ/AW6 copierColler in progress/AW6 safe/AW6 collerMieux.app -------------
kMDItemDisplayName = "AW6 collerMieux"
/Applications/AppleWorks 6/Généralités AppleWorks/Scripts/ƒboutonsƒ/AW6 copierColler in progress/AW6 safe/AW6 copierMieux.app -------------
kMDItemDisplayName = "AW6 copierMieux"
/Applications/AppleWorks 6/Généralités AppleWorks/Scripts/ƒboutonsƒ/AW6 copierMieux_2.app -------------
kMDItemDisplayName = "AW6 copierMieux_2"
/Applications/AppleWorks 6/Généralités AppleWorks/Scripts/ƒboutonsƒ/SS 2 décimales +.app -------------
kMDItemDisplayName = "SS 2 décimales +"
/Applications/AppleWorks 6/Généralités AppleWorks/Scripts/ƒboutonsƒ/SS cellules rose.app -------------
kMDItemDisplayName = "SS cellules rose"
/Applications/AppleWorks 6/Généralités AppleWorks/Scripts/ƒboutonsƒ/SS pasteValuesOnlyGUI.app -------------
kMDItemDisplayName = "SS pasteValuesOnlyGUI"
/Applications/AppleWorks 6/Généralités AppleWorks/Scripts/¢ DB ouvre un dossier PJ.app -------------
kMDItemDisplayName = "¢ DB ouvre un dossier PJ"
/Applications/AppleWorks 6/Généralités AppleWorks/Scripts/¢ DB ouvre_un_fichier10.app -------------
kMDItemDisplayName = "¢ DB ouvre_un_fichier10"
/Applications/AppleWorks 6/Généralités AppleWorks/Scripts Support/Show:Hide in Scripts Menu -------------
kMDItemDisplayName = "Show/Hide in Scripts Menu"
/Applications/AppleScript/Script Editor.app -------------
kMDItemDisplayName = "Éditeur de scripts"
kMDItemVersion = "2.1.1"
/Applications/AppleWorks 6/AppleWorks 6.app -------------
kMDItemDisplayName = "AppleWorks 6"
kMDItemVersion = "6.2.9"
/Applications/AppleWorks 6/AppleWorks_6 -------------
kMDItemDisplayName = "AppleWorks_6"
/Applications/AppleWorks 6/Compléments AppleWorks/Install Dataviz® DocumentsToGo® -------------
kMDItemDisplayName = "Install Dataviz® DocumentsToGo®"
/Applications/AppleWorks 6/Généralités AppleWorks/Éditeur d’équation/Carbon/Equation Editor -------------
kMDItemDisplayName = "Equation Editor"
kMDItemVersion = "3.8"
/Applications/AppleWorks 6/Généralités AppleWorks/Éditeur d’équation/Carbon/Equation Editor.UK -------------
kMDItemDisplayName = "Equation Editor.UK"
kMDItemVersion = "3.8"
.

Yvan KOENIG

It appears that the wrongdoer is this part of the shell script:

" | /usr/bin/colrm 1 22" 

With it, the returned data is wrongly truncated:
tell current application
path to applications folder
alias “Caviar Macintosh HD:Applications:”
do shell script “/usr/bin/mdfind -0 -onlyin ‘/Applications/’ "kMDItemContentTypeTree == ‘com.apple.application*’" | /usr/bin/xargs -0 /usr/bin/mdls -name kMDItemDisplayName -name kMDItemVersion | /usr/bin/colrm 1 22”
"s/Disk Utility.app -------------
Utilitaire de disque"
10.5.6"
s/Installer.app -------------
Programme d’installation"
2.1.9"
s/Network Utility.app -------------
Utilitaire de réseau"
1.4.2"
s/System Profiler.app -------------
Informations Système"
4.1.1"
s/Terminal.app -------------
Terminal"
1.5.1"
s/VoiceOver Utility.app -------------
VoiceOver Utility"
1.0"
ks 6/Généralités AppleWorks/Scripts/Ostrea/collervaleursseules.app -------------
collervaleursseules"
ks 6/Généralités AppleWorks/Scripts/Ostrea/encadrertotaux2.app -------------

Hello

" | /usr/bin/colrm 1 22"

removes characters 1 thru 22 of every rows of the returned datas.
Alas, a correct code would remove
1 thru 24 on rows 1+(n3)
1 thru 21 on rows 2+(n
3)
1 thru 20 on rows 3+(n*3)
with n starting from 0

Yvan KOENIG

It’s the version of mdls that comes with Tiger, which defaults to including the file name in the output (this isn’t even an option on Leopard).

This would all be a non-issue if the -raw option work the way I want it to, but unfortunately it doesn’t.

Yvan, try something like this:

set someFolder to path to applications folder

do shell script "/usr/bin/mdfind -0 -onlyin " & ¬
	quoted form of POSIX path of someFolder & ¬
	" \"kMDItemContentTypeTree == 'com.apple.application*'\"" & ¬
	" | /usr/bin/xargs -0 " & ¬
	" /usr/bin/mdls -name kMDItemDisplayName -name kMDItemVersion" & ¬
	" | /usr/bin/grep '^kMD.*[^-]$'" & ¬
	" | /usr/bin/colrm 1 22" & ¬
	" | /usr/bin/ruby -e " & ¬
	"'items = STDIN.readlines; items.each_index {|i| items[i].chomp!().chomp!(\"\\\"\"); print((i.modulo(2) == 0 ? items[i] : (items[i] != \"null)\") ? \" \"+items[i]+\"\\n\" : \"\\n\")) }'" & ¬
	" | /usr/bin/sort --ignore-case"
set appList to paragraphs of result

Hello

Thanks but it gives exactly the same error message.

As I wrote in my late messages, it seems that the problem is linked to the use of colrm.

If I disable the piece of code starting from
" | /usr/bin/colrm 1 22"

I get:

tell current application
path to applications folder
alias “Caviar Macintosh HD:Applications:”
do shell script “/usr/bin/mdfind -0 -onlyin ‘/Applications/’ "kMDItemContentTypeTree == ‘com.apple.application*’" | /usr/bin/xargs -0 /usr/bin/mdls -name kMDItemDisplayName -name kMDItemVersion | /usr/bin/grep ‘^kMD.*[^-]$’”
"kMDItemDisplayName = "Utilitaire de disque"
kMDItemVersion = "10.5.6"
kMDItemDisplayName = "Programme d’installation"
kMDItemVersion = "2.1.9"
kMDItemDisplayName = "Utilitaire de réseau"
kMDItemVersion = "1.4.2"
kMDItemDisplayName = "Informations Système"
kMDItemVersion = "4.1.1"

To clean that we have to remove
21 chars on odd rows
20 chars on even ones

Maybe grep may be used to replace kMDItemVersion by kMDItemVersionK so that colrm will be called to remove 21 chars on every row.

Yvan KOENIG

Sorry, I’m not interested in making this working on Tiger (especially when I can’t test it myself).

Thanks