Support With Sharing AppleScriptObjC Variable To Label Object - xcode

script AppDelegate
property theWindow : missing value
property displayLabel : missing value
on applicationWillFinishLaunching:aNotification
display dialog "Are you ready?" buttons {"Yes", "No"} default button "Yes"
end applicationWillFinishLaunching:
if result = {button returned:"No"} then
display alert "That's a shame."
tell application "This Application"
quit
end tell
else if result = {button returned:"Yes"} then
set testVariable to "this is a test"
end if
end script
So, this is an example of an app I'm working on in AppleScriptObjC. I've taken a lot out and replaced things with different strings, but this is the basic layout.
How can I get the value of 'testVariable' into the 'displayLabel' property, which is linked to a label in the interface builder? The label is empty initially, but I want the value of 'testVariable' to populate it once the script has been run.
Thanks!!!

The syntax is pretty similar to the ObjC syntax
ObjC
displayLabel.stringValue = testVariable;
AppleScriptObjC
set displayLabel's stringValue to testVariable

Related

MacOS: How to update an AppDelegate property via editing a table view cell?

In my macOS app I’m trying to update an AppDelegate property by editing a Table View Cell.
I created a sample project (Table View example project with array controller) using tips from the answer to this post.
I’m using macOS Big Sur (11.6.8) and Xcode 12.5.1. I'll be glad to supply a source archive download link or email it if requested. When I tried to add a link in this post it was rejected.
Here's my AppDelegate script:
script AppDelegate
property parent : class "NSObject"
property msg : ""
-- IBOutlets
property theWindow : missing value
property tableViewData : missing value
property arrayController : missing value
property showTableViewData : missing value
on `applicationWillFinishLaunching_(aNotification)`
appInit()
end applicationWillFinishLaunching_
on applicationShouldTerminate_(sender)
return current application's NSTerminateNow
end applicationShouldTerminate_
on appInit()
set my msg to (current date & return & return & "Initializing…") as string
delay .5
initTableView()
set my msg to (current date & return & return & "Ready.") as string
delay .5
end appInit
on initTableView()
set my tableViewData to {}
set my tableViewData to {{adName:("C21 ad" as string), pageNumber:("001" as string)}, {adName:("ERA ad" as string), pageNumber:("002" as string)}}
end initTableView
on `showTableViewData_(sender)`
set my msg to (current date & return & return & "showing tableViewData....") as string
delay .5
set displayText to ""
repeat with thisRecord in tableViewData
display alert "adName: " & (adName of thisRecord as string) & return & "pageNumber: " & (pageNumber of thisRecord as string)
end repeat
set my msg to (current date & return & return & "Ready.") as string
delay .5
end `showTableViewData_`
end script
How it works
As you can see I have hardcoded two records which I use to populate the tableViewData property on initialization which in turn is displayed in the table view. For some reason the adName data is not being displayed, but that bug is not what this post is about.
App initialized
Clicking the button reads the tableViewData property, iterates the records displaying them via an alert (so we can see it's contents).
Record display
Aside from the adName not displaying, so far so good.
Next, I edit the pageNumber table view cell for the first record (changing it from "001" to "777" and hitting return). When I click the button the first record is displayed which still shows a pageNumber value of "001" (instead of the "777" value I entered).
Record display after editing
Here are some shots of the table view cell attributes, connections, and bindings:
Attributes
Connections
Bindings
I tried selecting the bindings setting "Continuously Updates Value" but that doesn't seem to help; my tableViewData property is not updated with the newly entered data in the table view cell.
Most of the current developer docs use Obj-C or Swift instead of AppleScript and iOS or tvOS related. I'm using Xcode 12.5.1 because of problems binding UI elements to my code.
Thanks for taking the time to look this over.
The contents of tableViewData doesn't change but content of arrayController does. Try
repeat with thisRecord in content of arrayController
display alert "adName: " & (adName of thisRecord as string) & return & "pageNumber: " & (pageNumber of thisRecord as string)
end repeat
To make it work, use a Script Object instead of a record.
on makeAd(nameOfAd, pageNr)
script ad
property adName: nameOfAd
property pageNumber: pageNr
end script
return ad
end makeAd
on initTableView()
set my tableViewData to {makeAd("C21 ad", "001"), makeAd("ERA ad", "002")}
end initTableView

How to create splash screen with animated .gif image?

I would like to know if there is any way that when opening my App, a splash screen with an animated GIF image is presented and soon after this screen closes and opens the main screen of the app, what I have done so far is what is in the gif below, but to work I need to use this display alert , or otherwise I don't see the progress bar being loaded, that is, it already opens in the second tab of my tab view.
Example of what I've done so far.
script AppDelegate
property parent : class "NSObject"
-- IBOutlets
property theWindow : missing value
property gifImg : missing value
property indicatorProgress : missing value
property myTabView : missing value
property myLabel : missing value
on applicationWillFinishLaunching_(aNotification)
myTabView's selectTabViewItemAtIndex:0
loadGifScreen()
end applicationWillFinishLaunching_
on applicationShouldTerminateAfterLastWindowClosed_(sender)
return true
end applicationShouldTerminate_
on loadGifScreen()
display alert "" giving up after 2
set c to 0
repeat 100 times
set c to c + 1
delay 0.04
tell indicatorProgress to setDoubleValue:c
if c > 99 then
exit repeat
end if
end repeat
myTabView's selectTabViewItemAtIndex:1
end loadGifScreen
end script

Pressing cancel in choose from list will cause a error

When I execute this script, everything works fine, but then when I press the cancel button, the script just give me a error, "Can’t get item 1 of false"
set mailList to {"Hide Applications", "Quit Applications", "Full Volume"}
set mailType to choose from list mailList
if item 1 of mailType is "Hide Applications" then
tell application "Finder"
set visible of every process whose visible is true and name is not "Finder" to false
set the collapsed of windows to true
end tell
else if item 1 of mailType is "Full Volume" then
set volume output volume 100
else if item 1 of mailType is "Quit Applications" then
tell application "System Events" to set the visible of every process to true
set white_list to {"Finder"}
try
tell application "Finder"
set process_list to the name of every process whose visible is true
end tell
repeat with i from 1 to (number of items in process_list)
set this_process to item i of the process_list
if this_process is not in white_list then
tell application this_process
quit
end tell
end if
end repeat
on error
tell the current application to display dialog "An error has occurred!" & return & "This script will now quit" buttons {"Quit"} default button 1 with icon 0
end try
end if
You add a check if mailType is a boolean which would happen if you press cancel. The check would be inserted between line 2 and 3 and would look something like this:
...
if class of mailType = boolean then return
...
choose from list returns boolean false when the Cancel button is pressed.
Just abort the script immediately in this case
set mailType to choose from list mailList
if mailType is false then return

Populate text field with variable and show text

Here's my code:
set todayDate to do shell script "date '+%Y/%m/%d'"
set salesRef to "COMPANY-AB" & "-" & theCustomerInitials & "-" & todayDate & "-" & theCustomerID
set the clipboard to salesRef as text
display dialog salesRef as text buttons {"OK"} default button 1
Here's the UI: http://i.imgur.com/sfRnpr6.png
I'd like to make the text 'Copied!' appear when the successfully completes the clipboard line, but remain hidden until then, and also populate the text field at the bottom with the 'salesRef' variable, so the user get's an output.
Essentially then, I'll be able to remove the display dialog. But I can't work out how to do this :(
Successfully managed this by adding these lines:
property theTextField : missing value
…
theTextField's setStringValue:salesRef
Then created a connection to the text field in IB by control dragging from the App Delegate to the text field, and selecting the property.
Are you ok with NSAppleScript?
if so here is an example:
NSAppleScript *myScriptThatShouldGimmeStuff = [[NSAppleScript alloc] initWithSource:
#"return (text returned of (display dialog \"gimme your input:\" default answer \"\" buttons {\"Ok\"} default button 1))"];
//this previous line is the applescript
NSAppleEventDescriptor *theStuff = [myScriptThatShouldGimmeStuff executeAndReturnError:nil];
self.myTextBoxToPutInputIn.stringValue = [theStuff stringValue]; //[theStuff stringValue] is the result returned
/|\
/ | \
/ | \
|
|
//don't forget to change this
//(I didn't comment out the arrow because i want xcode
// to throw you an error so you don't forget to change that).
So it is not clear from you code what you are doing, but IF you are using ASOC, then you have to define OUTLETS to the controls in your UI, and then you just set the .stringValue of the outlet to whatever string you want to appear in them.
If you don't know how to set up outlets, watch this video and download the example code (look for INTRO VIDEO on the page):
http://www.macosxautomation.com/applescript/develop/index.html
Or for another step-by-step example, here:
http://asobjcresources.weebly.com/getting-a-text-field-input.html

The variable result is not defined AppleScript

I am working on the same app that I mentioned in my first question. I have gotten much farther, but when I try to play "2048" the first time, AppleScript give me the error:
"The variable result is not defined"
I will skip the main bulky body of the app and get to the area with the problem:
display dialog "What would you like to do?
Sleep = Go to sleep
Finder = Open Finder
Time = Display current time and date
2048 = play 2048
Quit = Quit application" default answer "" with title "Control panel"
if the text returned of the result is "Sleep" then
tell application "System Events" to sleep
display dialog "Hello, welcome back!" buttons {"Thank you!"} default button 1
return
else if the text returned of the result is "Finder" then
tell application "Finder" to make new Finder window
else if the text returned of the result is "Time" then
set a to (current date) as list
set AppleScript's text item delimiters to linefeed
set a to a as text
display dialog a buttons {"OK"} default button 1
else if the text returned of the result is "Quit" then
return
else if the text returned of the result is "2048" then
tell application "Terminal"
do script "/Users/student/Documents/2048.sh"
activate
end tell
else
display dialog "Invalid response" with title "Invalid response" buttons {"Go back", "Quit"} default button 1
end if
if the button returned of the result is "Go back" then
display dialog "What would you like to do?
Sleep = Go to sleep
Finder = Open Finder
Time = Display current time and date
2048 = play 2048
Quit = Quit application" default answer "" with title "Control panel"
else
return
end if
if the text returned of the result is "Sleep" then
tell application "System Events" to sleep
display dialog "Hello, welcome back!" buttons {"Thank you!"} default button 1
return
else if the text returned of the result is "Finder" then
tell application "Finder" to make new Finder window
else if the text returned of the result is "Time" then
set a to (current date) as list
set AppleScript's text item delimiters to linefeed
set a to a as text
display dialog a buttons {"OK"} default button 1
else if the text returned of the result is "Quit" then
return
else if the text returned of the result is "2048" then
tell application "Terminal"
do script "/Users/student/Documents/2048.sh"
activate
end tell
end if
Just set the result of the display dialog to a variable and use the variable. It's tricky to use "result" as you have in many if statements. One of them is bound to cause a problem because "result" will change at some point.
So do something like this right after your display dialog statement...
set {buttonReturned, textReturned} to {button returned of result, text returned of result}
Then in your if statements use buttonReturned or textReturned.

Resources