Newbie: Extract Version number from a string

Hi all!

I have created a simple script that extacts the version number of an app as below:-


tell application "Finder"
	set appname to alias "Internal 60GB:Applications:Net Monitor.app:"
	set ver2 to version of appname
	set ver3 to "Your version of Net Monitor is " & ver2
	display dialog ver3
end tell

This outputs:-

However, I would like to extract just the number so it would read like:-

How would I do this in a way that will also work for whatever application is selected as different apps will produce different strings.

Many Thanks
Sy

You’re really looking for the file’s ‘short version’, which I don’t think the Finder can read. If the application file’s an old-style file with a resource fork, you can get the short version using ‘info for’ from the Standard Additions. Otherwise — on my machine at least — if the file’s really a package, you have to parse the package’s Info.plist file. There may be a shell script that can do this, but I’m not au fait with those.

This isn’t very nice, but it works in OS 10.2.8:

set appPath to (path to "apps" as string) & "Net Monitor.app"
set appInfo to (info for file appPath)
if appInfo's package folder then
  -- Parse the Info.plist file.
  set plistText to (read file (appPath & ":Contents:Info.plist"))
  set astid to AppleScript's text item delimiters
  set AppleScript's text item delimiters to "CFBundleShortVersionString</key>"
  set shortVersion to word 4 of text item 2 of plistText
  set AppleScript's text item delimiters to astid
else
  -- Read the short version from the 'info for' file information record.
  set shortVersion to appInfo's short version
end

display dialog "Your version of Net Monitor is " & shortVersion

that is exactly what I wanted! Worked perfectly, THANKS!!!

Hi, greggo. Thanks for that. But it doesn’t work in Jaguar (AppleScript 1.9.1). On this system, the record returned by ‘info for’ only has a ‘short version’ property if the subject file has a version resource. There’s no such property when the file’s a package. That’s why I offered the ‘if’ block method above. Your simpler script obviously works for you and sy, presumably with some version of Panther, so it’s good to know that that particular problem’s been fixed in later versions of AppleScript.

The Finder ‘tell’ block isn’t necessary in Jaguar or earlier, but the automatic insertion of the disk name works a treat. :slight_smile:

I suppose that for portability, a ‘try’ block’s what’s needed:

set appFileName to "Mail.app"
set appPath to ":Applications:" & appFileName
try
  set verNum to short version of (info for file appPath )
on error number -1728
  set astid to AppleScript's text item delimiters
  try
    set plistText to (read file (appPath & ":Contents:Info.plist"))
    set AppleScript's text item delimiters to "CFBundleShortVersionString</key>"
    set verNum to word 4 of text item 2 of plistText
  on error
    ser verNum to "anyone's guess."
  end try
  set AppleScript's text item delimiters to astid
end try

display dialog "Your version of " & appFileName & " is " & verNum with icon note

I’ll have to rewrite one of my scripts in ScriptBuilders. :slight_smile: