Hi all. The functionality I am looking for is this: I want is to be able to run an AppleScript from Cocoa (using NSAppleScript) and be able to find out what the current application is. My script is executed from a Cocoa installed menubar item, and so the current application is the app that the user is seeing when they choose from my menu.
I tried this for the AppleScript:
tell current application
return my name
end tell
but this returns the name of my cocoa app…not the name of what I am calling the “current application”.
I also tried looking at:
get name of every process
But this returns a list of processes that don’t seem to be in order of recent use or anything useful to me.
path to frontmost application --> alias "path:to:app"
name of (info for (path to frontmost application)) --> "app"
tell application "Finder" to name of (first process whose frontmost is true) --> "app"
But now I’m trying to figure out how I could have come up with the solution myself. So, my question is, how do you know this? I tried looking in the Finder dictionary and found a “frontmost” reference, but it apparently returns a boolean as to whether or not the Finder is the frontmost application. So where should I have looked?
Also, where can I get type information for “functions”? To write your (second version) code in more standard prog lang-ese:
name(info(path ‘frontmost application’))
Then I assume that ‘frontmost application’ returns an application object, PATH takes an application object and returns a string path, INFO takes a path and returns some kind of information object about the file referenced by the path, and finally, NAME takes an info object and returns a string which is the app’s name.
Am I right about this? Where do I find this info in the case that I had to come up with this code myself. Again, thanks a lot.
You will find lots of useful info and all (or nearly all) available commands into “dictionaries”. There are several places to find a dictionary. The two common ones are applications and scripting additions (or “osaxen”).
The quick way to find dictionaries is dropping the icon of your app onto the script editor’s own icon. Most probably, you have also such options under the “File” menu of your favorite script editor. Also, most of them (Smile, Script Editor or Script Debugger) have additional palettes or shortcuts to open them more easily than browsing your whole disk.
As you guessed, I used various “commands” (from the scripting addition “Standard Additions” and the Finder), such as “path to” or “info for”. When I request a “name” or make a reference to a “process”, I’m targetting “classes” and “properties” of such dictionaries, owned by the related “objects”.
Perhaps you wish take a look to our FAQ, where you’ll find some useful info about dictionaries, osaxen and interesting stuff to get started with AppleScript. http://www.macscripter.net/faq/