How do I populate a popup button with AppleScript? - applescript

I've found this snippet of code in various place around the 'net:
tell window 1
tell menu of popup button 1
delete every menu item
repeat with catListItem in catList
make new menu item at end of menu items with properties {title:catListItem}
end repeat
end tell
end tell
When I use it in my AppDelegate script in a Cocoa-AppleScript Application, Xcode gives me an error: *t2t_AppDelegate.applescript:25: error: Expected end of line but found identifier. (-2741)* (Line 25 is "tell menu...")
I'm not sure what I'm missing that would allow me to dynamically populate the popup button with a list of terms (catList) that I'm drawing from another application. Any suggestions?

Unless you are running something earlier than Snow Leopard, It looks like you are using AppleScript Studio terminology (which was deprecated in Snow Leopard). Using the current AppleScriptObjC framework, the user interface items are referenced via outlet properties, for example:
property myPopUp : missing value
From the Interface Editor, this property is connected to your popup button, which allows you to use it with various methods in the NSPopupButton class and its parents, such as addItemsWithTitles. Once everything is defined and connected, you would use something like:
set catList to {"next item", "another item", "Items added"}
myPopUp's addItemsWithTitles_(catList)

Related

Delphi FMX TMainMenu first menu item doesn't show in OSX, OK in WIndows

I created a Multi-Device Application in Delphi Seattle and added a TMainMenu with MenuItem1 and MenuItem2 (nothing else). Building and running for OSX, only MenuItem2 shows. Building and running for Windows, both menu items show. I didn't treat the two menu items any differently. Both were just added and not customized in any way. Anyone else experience this?
The top level items in a Mac menu bar typically look like this:
Apple/system menu ('About this Mac', 'System Preferences...', etc.)
Application menu ('About XXX', 'Services', 'Hide XXX', 'Hide Others',
'Show All', 'Quit XXX')
File menu
Edit Menu
...
In FMX (excepting the very first version), the first top level item in a TMainMenu represents the application menu. As such, you need to add an item before File (or whatever is your current first item) that is only show on OS X; its sub-items should then include the usual items of a Mac application menu. To implement those items' behaviour, you can utilise standard actions (TFileHideApp, TFileHideAppOthers, TFileExit). Or, in more detail:
If there isn't one already, add a TActionList component to the form
Double click the action list to bring up the action list editor
Click on the add button's dropdown arrow, and select New Standard Action...
Select all of TFileHideApp, TFileHideAppOthers and TFileExit, and click OK
Back on the form, open up the menu editor by double clicking on the TMainMenu component
Add an item immediately before the first one that currently exists; in the Object Inspector, call it (say) mnuApp
Add at least four child items to mnuApp; using the Object Inspector, assign the Action property of the first to FileHideApp1, the second to FileHideAppOthers, and the fourth to FileExit1; set the Text of the third to a hyphen (-) to make it a separator
Close the menu editor
If one doesn't already exist, create a OnCreate handler for the form by double clicking its entry in the Object Inspector
Add to FormCreate a line to hide mnuApp if not running on OS X. Conversely, if you already have a menu item for File|Exit, this should be hidden if running on OS X since the functionality is now Quit under the application menu:
...
procedure TForm1.FormCreate(Sender: TObject);
begin
mnuApp.Visible := (TOSVersion.Platform = pfMacOS);
itmExit.Visible := (TOSVersion.Platform <> pfMacOS);
end;

Xcode Applescript create list of results

I have been trying to work out the best way to accomplish this and every time I think I am on to something it doesn't seem to work in my situation.
If someone could point me in the right direction to an existing example or the correct google term I would be very grateful.
I am creating a Cocoa - Applescript Application in Xcode 5.
I have the basics in already, which so far prompts the user to select an audio track, the track is then played back in Quick Time and I have a button to return the current play time of the track, as it stands at the moment this returns the time to a variable of SMPTE ala.
tell application "QuickTime Player"
set SMPTE to get current time of document 1
end tell
My problem is what to do with the result of "SMPTE"
I would like to generate a list in a separate window with each button press updating a new row with the new returned value.
I have tried using a NSTableColumn but can't work out how to "auto fill" with each subsequent button press.
A very simplistic way.
Add an NSArrayController to the IB Objects.
Connect it to an outlet.
Select the TableView's TableColumn and go to it Bindings Inspector.
Bind it's Value to Array Controller
And name it's model key path as 'time'
In the TableColumn's Attributes Inspector. Set it's Title to 'time'
The code would be like this:
property ArrayController :missing value
on addTime_(sender) --clicked to add time
tell application "QuickTime Player"
set SMPTE to get current time of document 1
end tell
ArrayController's addObject:{|time|:SMPTE}
end addTime_
The Button would need to be connected to the Action: addTime:
To remove an item:
Simply add a new button and connect it to the ArrayController's Remove Action method.
( By dragging the button's connection to the ArrayController object in IB and selecting remove: )
To Save the data to be able to see it on relaunch:
Select the ArrayController object in IB and go to it's bindings inspector.
Bind it's Control Content's content Array to the 'Shared user defaults Controller'
And name it's model key path as 'theValues'
There is a very good intro tutorial here
And NSArrayController's Docs

How can I get text from a text field with an applescript xcode project

I am trying to write an xcode app with applescript. I need to get user inputed text from a text box and set is as a variable when the submit button is pressed.
Thank You
I too am just getting to grips with Cocoa-Applescript (and there's not all that much info around!) but it should work as follows, at least this is how I've done it in my app :)
1) Create an outlet for the text field:
i.e. underneath:
script AppDelegate
property parent : class "NSObject"
add:
property myTextField : missing value
The 'missing value' bit is what makes myTextField available in Interface Builder.
2) Create a handler for your button:
on submitButtonClicked_(sender)
end submitButtonClicked_
(name it anything you like, but as far as I know it has to start with a lowercase letter and end with an underscore)
3) Link the handler and outlet to their respective UI elements:
In Interface Builder, right-click the App Delegate, and drag the circle next to submitButtonClicked onto your button
Likewise drag the circle from myTextField to the text field
4) Add a line in the click handler to set a variable to the value of the field:
on submitButtonClicked_(sender)
set currentTextFieldText to myTextField's stringValue() as text
-- do stuff with currentTextFieldText
end submitButtonClicked_
And if you wanted to put something in the text field:
tell myTextField to setStringValue_(whatYouWantInTheField)
Hope this helps!

Define the class of the sender when making an IBAction connection in Xcode 4,2.1

In Xcode 4 or above, it has a handy function allowing us to CTRL + drag an object from the interface to the .h file to quickly connect the object with an event method (assume the Assistant Editor is enabled).
Say we have an UIButton in the interface, and we want to add an IBAction for its "touch up inside", we enable the assistant window and press/hold CTRL + Drag the button to the .h file to quickly generate the necessary codes.
In the popup prompt box, say we set "connection" as "Action".
In the "Type" drop-down, we can select "id" or "UIButton". <--- this is where my problem is.
The strange thing in Xcode 4.2.1 is: no matter what I select, it always generates code: "(id)sender" as the argument.
I know it is easy to manually change it to "(UIButton *) sender", but what is the point of this drop-down when it always generates "(id)"?
Is this is a bug of Xcode or am I missing something to make it directly generate the code "(UIButton *) sender" when I select "UIButton" in this drop-down?
Edited on 27/Feb/2012: This is confirmed solved in Xcode 4.3
- (void)action:(id)sender is just the way actions are defined. you can, in theory, connect different UI elements to the same action. after you've created the connection, you can manually change id to whatever class you want, or just do a cast inside the method.

How do I move first responder with applescript?

I want to address the following annoyance with iTunes: After I have searched for a track (by pressing cmd+opt+f to move to the search field) I want to be able to play the first track in the songs list. Ideally I would like cmd+enter to start playing the first track in the song list and also move the focus to the song list. For example I enter 'Highway 61' in the search box, press cmd+enter and 'Like a Rolling Stone' starts playing.
My initial idea is for an applescript that moves the focus from the search field to the song list, selects the first song and plays it.
Here's what I have:
tell application "iTunes"
set first responder of (window 1) to outline "songs"
end tell
When I try to run this script Applescript Editor throws a syntax error "Expected class name found identifier" and highlights responder. This script follows the same form as many of the applescripts I've found on the web. What am I doing wrong?
Aside/rant: Applescript is the most frustrating and confusing technology I've ever had the stupidity to inflict upon myself. I hate it. I hate it. I hate it. I hate it.
Applescript's syntax is idiosyncratic, but it's not bad in the sense that you have a uniform scripting language for GUI. This was (and still is) something amazing. The strange sytanx is also not that strange once you go through Apple's language definition which can be found here.
That said, you need to open the AppleScript Dictionary of each app to see what kind of nouns and verbs are defined.
Think of an app as a library of classes and methods from the point of view of the AppleScript. Of course you can't call a method which is not defined in a library, right?
Launch AppleScript Editor, go File→Open Dictionary, and choose iTunes. You soon find that there's no such noun as first responder is defined.
The point is, the app usually only exposes its internal object structure, not every element of the UI. But usually that's enough.
If what you want to do cannot be done using the proper Applescript interface an app exposes, as a last resort, you can directly manipulate the UI elements, using a helper app called "System Events".
So, go File→Open Dictionary again, and this time choose "System Events", and check the content of "Processes Suite." It allows you to manipulate the UI. For example, to get the list of all UI elements, use
tell application "System Events"
tell application process "iTunes"
get UI elements of window 1
end tell
end tell
Have fun! Applescript looked horrible to me for a while, but it's not a bad thing once I learned I need to always refer to the dictionary.
Mac OS X Automation is a great source of tutorials.
If I understand correctly, you don't need an AppleScript to do that. Just press the Tab key to move the focus between elements. In my case, for example, pressing Tab moves the focus from the search box to the left-side selection bar then to the main list of songs then back to the search box again.
EDIT Addressing your further refinement of the question: you can get from searching to playing with two key strokes - TAB to move the focus out of the search box to the song list, then SPACE to play the first track in the selection. You can also use the arrow keys to pick a different selection.
Just for fun, though, here's one way you could do the whole sequence in AppleScript:
tell application "iTunes"
play first item of (search first library playlist for "Highway 61")
end tell
AFAIK, iTunes doesn't implement the command set first responder of. Applescript studio implements such command.

Resources