Tell Application WHAT ? - a script for the perplexed ...

A script for those moments when you can’t remember the creator code, bundle identifier, or full .app name of one of the applications you want to automate.

(Compatible with Launchbar argument handling … tap the spacebar and give it part of the name of a running application)

-- Get creator codes and bundle identifiers for tell applications blocks
-- (for applications currently running
-- Ver .05 Compatible with LaunchBar - select script, tap space bar and enter string in Launchbar

property pTitle : "Tell application id ..."

property pDefaultSearch : "omni"

property pCodeCopy : "Copy creator code"
property pBundleCopy : "Copy bundle identifier"
property pNameCopy : "Copy app name"

-- System Events
on run
	tell application id "sevs"
		activate
		set strSearch to text returned of (display dialog "Enter part of application name:
   
(or leave blank to see all running applications)" default answer pDefaultSearch with title pTitle)
	end tell
	
	handle_string(strSearch)
end run

on handle_string(strSearch)
	tell application id "sevs"
		
		if (strSearch ≠ "") and (strSearch ≠ "*") then
			set {lstCode, lstBundle, lstFile} to {creator type, bundle identifier, file} of (application processes where name contains strSearch and bundle identifier is not missing value)
			set strPrompt to "Applications with \"" & strSearch & "\" in their name:"
		else
			set {lstCode, lstBundle, lstName, lstFile} to {creator type, bundle identifier, name, file} of (application processes where bundle identifier is not missing value)
			set strPrompt to "All currently running applications:"
		end if
		repeat with i from 1 to length of lstCode
			set varFile to item i of lstFile
			if varFile ≠ missing value then
				set strName to (name of item i of lstFile)
			else
				set strName to item i of lstName
			end if
			
			set item i of lstCode to item i of lstCode & "=" & item i of lstBundle & "=" & strName
		end repeat
		
		set my text item delimiters to linefeed
		set strApps to lstCode as string
		
		set lstApps to paragraphs of (do shell script "echo " & quoted form of strApps & " | sort -t '=' -k 3")
		if length of lstApps > 0 then
			set lstDefault to {first item of lstApps}
		else
			activate
			display alert "No running apps have names matching " & strSearch
			return
		end if
		activate
		set varChoice to choose from list lstApps with prompt strPrompt default items lstDefault with title pTitle
		
		if varChoice is false then return
		
		set my text item delimiters to "="
		set {strID, strBundle, strName} to text items 1 thru 3 of first item of varChoice
		
		set {blnCode, blnBundle, blnName} to {true, true, true}
		
		-- CREATOR CODE
		if strID ≠ "????" then
			set strCode to "tell application id \"" & strID & "\""
		else
			set blnCode to false
			set strCode to strName & " has no creator code ..."
		end if
		
		-- BUNDLE IDENTIFIER
		if strBundle ≠ "missing value" then
			set strBundle to "tell application id \"" & strBundle & "\""
		else
			set blnBundle to false
			set strBundle to strName & " has no bundle identifier"
		end if
		
		-- APPLICATION NAME
		if strName ends with ".app" then
			set strName to text 1 thru -5 of strName
			set strAppName to "tell application \"" & strName & "\""
		else
			set blnName to false
			set strAppName to ""
		end if
		
		
		set strChoice to "CREATOR CODE:    " & strCode & "

BUNDLE IDENTIFIER:    " & strBundle & "

APP NAME:    " & strAppName
		
		
		set lstBtns to {}
		if blnName then set end of lstBtns to pNameCopy
		if blnBundle then set end of lstBtns to pBundleCopy
		if blnCode then set end of lstBtns to pCodeCopy
		set lngButtons to length of lstBtns
		
		if lngButtons > 0 then
			set strClip to "-- " & strName & return
			activate
			set strBtn to (button returned of (display dialog strChoice buttons lstBtns default button lngButtons with title strName))
			if strBtn = pCodeCopy then
				set strClip to strClip & strCode
			else if strBtn = pBundleCopy then
				set strClip to strClip & strBundle
			else
				set strClip to strAppName
			end if
			set the clipboard to strClip & return & return & "end tell"
		else
			activate
			display alert strName & " is not a scriptable process"
		end if
		set my text item delimiters to space
	end tell
end handle_string

Thanks for this, I’ve tried it but get an error every time I run the script. I’m using OS 10.9, maybe an incompatibility with the new script editor? Thanks for any help you can provide.

It looks as if System Events’s ‘whose’/‘where’ filter is trying to compare the bundle identifiers with ‘missing value’ textually instead of absolutely for some reason and failing miserably. The script can be made to work by omitting bundle identifiers from the filter but checking them in the repeat which immediately follows. So the top of the handle_string() handler would look like this:

on handle_string(strSearch)
	tell application id "sevs"
		
		if (strSearch ≠ "") and (strSearch ≠ "*") then
			set {lstCode, lstBundle, lstFile} to {creator type, bundle identifier, file} of (application processes whose name contains strSearch) -- Don't filter for bundle identifier.
			set strPrompt to "Applications with \"" & strSearch & "\" in their name:"
		else
			set {lstCode, lstBundle, lstName, lstFile} to {creator type, bundle identifier, name, file} of application processes -- Ditto.
			set strPrompt to "All currently running applications:"
		end if
		repeat with i from 1 to length of lstCode
			set bundleID to item i of lstBundle
			if (bundleID is not missing value) then -- Check bundle identifiers here.
				set varFile to item i of lstFile
				if varFile ≠ missing value then
					set strName to (name of item i of lstFile)
				else
					set strName to item i of lstName
				end if
				
				set item i of lstCode to item i of lstCode & "=" & bundleID & "=" & strName
			end if
		end repeat
		
		-- Rest of handler here.
		
	end tell
end handle_string

Hello.

Here is a different take, it only gives you the bundle identifer/ creator code for the app you ask for, so it obviously doesn’t work well with System Agents and the like. So this script isn’t a replacement, only a supplement of yiam-jin-qui script I find it pleasant though. when I know somehow what I am after, which in most cases is the bundle identifier of the frontomost app.

(*
Copyright © 2014 McUsr. All rights reseved.

18.02.14T13:24:09

Revised to include createor code.
*)
property scriptTitle : "Get BundleId of Frontmost App"
global creatCode, bundleId, toClip
set {creatCode, bundleId, toClip} to {"Creator Code", "Bundle Id", "To Clipboard"}
tell application id "com.apple.systemevents"
	set nm to name of first process whose frontmost is true
end tell

local wanted, appname
tell application id "com.apple.systemuiserver"
	set {wanted, appname} to {button returned, text returned} of (display dialog "Enter the name you want the bundle id for!" default answer nm with title scriptTitle with icon 1 buttons {"Cancel", creatCode, bundleId} cancel button 1 default button 3)
	if appname = "" then
		display dialog "I need an application name.quitting!" with title scriptTitle with icon 2 buttons {"Ok"} default button 1
		error number -128
	end if
end tell
if wanted is creatCode then
	set {failed, feedBack} to getcreatorcode for appname
else if wanted is bundleId then
	set {failed, feedBack} to getbundleid for appname
end if
tell application id "com.apple.systemuiserver"
	if failed = true then
		
		display dialog "I couldn't find a " & wanted & " for application: " & appname with title scriptTitle with icon 2 buttons {"Ok"} default button 1
		
	else
		set {theButton, forShow} to {button returned, text returned} of (display dialog "The " & wanted & "  for " & appname & " is: " default answer feedBack with title scriptTitle buttons {"Cancel", toClip, "Ok"} cancel button 1 default button 3 with icon 1)
		if theButton is toClip then
			set theButton to button returned of (display dialog "Please select an option:" with title scriptTitle buttons {"Cancel", "Hyphenated", "Tell Block"} cancel button 1 default button 3 with icon 1)
			if theButton is "Hyphenated" then
				set the clipboard to ("\"" & feedBack & "\"")
			else
				set the clipboard to ("tell application id \"" & feedBack & "\"

end tell")
			end if
		end if
	end if
end tell

on getbundleid for anAppName
	set {failed, foundBundleId} to {false, null}
	tell application id "com.apple.systemevents"
		try
			set foundBundleId to bundle identifier of application process anAppName
		on error
			set failed to true
		end try
	end tell
	return {failed, foundBundleId}
end getbundleid

on getcreatorcode for anAppName
	local failed, crea
	set {failed, crea} to {false, null}
	try
		tell application id "com.apple.systemevents"
			set crea to creator type of application process anAppName
		end tell
	on error
		set failed to true
	end try
	if crea ≠ null and crea is "????" then
		set failed to true
		set crea to ""
	end if
	return {failed, crea}
end getcreatorcode

Both very nice and handy. Thanks

Thank you Nigel and yiam-jin-qui, is there a way to filter the list by visible programs open, not just all open processes?