How to create splash screen with animated .gif image? - applescript

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

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

AppleScript - How do I detect if a specific button is clicked on GUI and terminate the script?

I have a script that manipulates a GUI. At some point in the process, a progress indicator appears in the GUI for a few mins. If the user clicks the GUI button button "Stop" of sheet 1 (so not a dialog button), I'd like the script to display dialog followed by error number -128. How do I do this? Here's what I tried...
repeat while progress indicator 1 of sheet 1 exists
try
set button_returned to button returned of button "Stop" of sheet 1
if button_returned is "Stop" then
display dialog "Operation cancelled"
error number -128
end if
end try
end repeat
Note: I use repeat while progress indicator 1 of sheet 1 exists to pause the script whilst the progress indicator is up.
Save this example as usual app to see the correct workaround to solve your problem:
global itemCount
set processName to name of current application
set theList to {"Marlow", "Maddie", "Sammy", "Stuey", "Jessie", "Tolstoy", "Marlow", "Maddie", "Sammy", "Stuey"}
set itemCount to count of theList
set progress total steps to count of theList
repeat with i from 1 to itemCount
set thisItem to item i of theList
set progress description to "Item " & i & " of " & itemCount
set progress additional description to thisItem
-- The delay is simply to simulate processing time
-- so you can see the progress bar in action.
-- Exclude this from your code and put your real do stuff.
delay 1
set progress completed steps to i
end repeat
on quit
if progress completed steps < itemCount then
display dialog "Operation cancelled"
end if
end quit

How Can I Show Progress/Countdown to Next Action In The Menubar Systemwide Applescript

I created an Applescript that every 600 seconds retrieve the last line of a log file and add it to a Google Sheet spreadsheet.
This is extremely useful, but it is starting to annoy me not knowing when the next action will be performed.
So now I'm trying to display the progress (or countdown in seconds) to the next action in the menubar, so I can, kind of, be aware that the focus application will change.
Can please someone point me in the right direction?
--- Added Information ---
Now, instead of showing a countdown, I want to just simply show how many steps left, and it's still not working. This is a test code I'm working on:
set progress total steps to 12
set progress completed steps to 0
set progress description to "Running..."
set progress additional description to "ETA: 120 Seconds"
repeat with a from 1 to 12
delay 10
set progress completed steps to a
set ETA to a * 10
set progress additional description to "ETA: " & ETA & " seconds"
end repeat
What I'm doing wrong?
Based on the link red_menace gave in comments and the screenshot you provided, here's a script that (I think) will give the results you want. It's not an automator workflow. This is an AppleScript stay-open application. Copy it into Script Editor and save is as an application: choose "Application" from the File Format pulldown menu on the save screen, and make sure you check the Stay open after run handler checkbox. then run the application like normal and see the demo.
use framework "AppKit"
use framework "Foundation"
use scripting additions
property ca : current application
property NSStatusBar : class "NSStatusBar"
property NSMenu : class "NSMenu"
property NSMenuItem : class "NSMenuItem"
property NSImage : class "NSImage"
property NSProgressIndicator : class "NSProgressIndicator"
property NSView : class "NSView"
property NSTextField : class "NSTextField"
property idle_time : 1
global status_bar_item, progress_views, idx
on run
set progress_views to {}
set status_bar_item to NSStatusBar's systemStatusBar's statusItemWithLength:(ca's NSSquareStatusItemLength)
set status_bar_item's |menu| to NSMenu's alloc's initWithTitle:""
set status_bar_item's |menu|'s minimumWidth to 220
my setImage(ca's NSImageNameSmartBadgeTemplate)
my addToStatusMenu(my progress_view_obj("Number 1"))
my addToStatusMenu(my progress_view_obj("Number 2"))
set idx to 0
end run
on idle
set idx to idx + 1
repeat with this_obj in progress_views
set rand_numb to (random number from 1 to 9)
(this_obj's progress_indicator's incrementBy:rand_numb)
this_obj's changeLabel("Adding: " & rand_numb)
end repeat
if idx > 20 then
quit
end if
return idle_time
end idle
on quit
-- remove status item and quit
NSStatusBar's systemStatusBar's removeStatusItem:status_bar_item
continue quit
end quit
on addToStatusMenu(obj)
set statusMenu to status_bar_item's |menu|
statusMenu's addItem:(obj's menu_item)
set end of progress_views to obj
end addToStatusMenu
on progress_view_obj(label_value)
script prog_view_obj
-- text field
property progress_indicator : missing value
property menu_item : missing value
property label_field : missing value
global content_view
-- make label field
set label_field to NSTextField's labelWithString:label_value
set label_field's translatesAutoresizingMaskIntoConstraints to false
-- progress indicator
set progress_indicator to NSProgressIndicator's alloc's initWithFrame:(ca's NSMakeRect(0, 0, 200, 20))
set progress_indicator's indeterminate to false
progress_indicator's setStyle:(ca's NSProgressIndicatorStyleBar)
progress_indicator's startAnimation:me
set progress_indicator's translatesAutoresizingMaskIntoConstraints to false
-- content view for menu item
set content_view to NSView's alloc's initWithFrame:(ca's NSMakeRect(0, 0, 200, 100))
set content_view's translatesAutoresizingMaskIntoConstraints to false
content_view's addSubview:label_field
content_view's addSubview:progress_indicator
-- constraints to arrange elements
--set view_height_constraint to content_view's heightAnchor's constraintEqualToConstant:100
--view_height_constraint's setActive:true
set lf_left_margin to content_view's leadingAnchor's constraintEqualToAnchor:(label_field's leadingAnchor) |constant|:-10
lf_left_margin's setActive:true
set pi_left_margin to content_view's leadingAnchor's constraintEqualToAnchor:(progress_indicator's leadingAnchor) |constant|:-10
pi_left_margin's setActive:true
set pi_right_margin to content_view's trailingAnchor's constraintEqualToAnchor:(progress_indicator's trailingAnchor) |constant|:10
pi_left_margin's setActive:true
set top_spacing_const to content_view's topAnchor's constraintEqualToAnchor:(label_field's topAnchor) |constant|:-10
set mid_spacing_const to label_field's bottomAnchor's constraintEqualToAnchor:(progress_indicator's topAnchor) |constant|:-10
set bott_spacing_const to content_view's bottomAnchor's constraintGreaterThanOrEqualToAnchor:(progress_indicator's bottomAnchor) |constant|:10
top_spacing_const's setActive:true
mid_spacing_const's setActive:true
bott_spacing_const's setActive:true
content_view's updateConstraints()
-- create and flesh out menu item
set menu_item to NSMenuItem's alloc's init()
set menu_item's view to content_view
content_view's updateConstraints()
on changeLabel(label_value)
set label_field's stringValue to label_value
end changeLabel
on changeMinValue(val)
set progress_indicator's minValue to val
end changeMinValue
on changeMaxValue(val)
set progress_indicator's maxValue to val
end changeMaxValue
on changeValue(val)
set progress_indicator's doubleValue to val
end changeValue
on currentValue()
return progress_indicator's doubleValue() as integer
end currentValue
end script
run prog_view_obj
return prog_view_obj
end progress_view_obj
on setImage(imageName)
status_bar_item's button's setImageScaling:(ca's NSImageScaleProportionallyDown)
status_bar_item's button's setImage:(NSImage's imageNamed:imageName)
status_bar_item's button's setImagePosition:(ca's NSImageLeft)
end setImage
You shouldn't need to change anything except in the run and idle handlers — the first to set up the menus and the second to increment the progress bars or update the labels as needed. You can change how frequently the indicators update by increasing or decreasing the idle-time property (it's currently set to run the idle loop once a second). You'll only need to get into the big, messy handler if you need to change the layout or size of the menu items.
Technical point: I've put each progress view inside a script object (script prog_view_obj) to make it easier to work with. That may take some time for you to wrap your head around — it's tricky AppleScript — but at any rate, there's a bunch of convenience handlers at the end of the script object definition that you may find useful.

Can I read/write the name of the Illustrator artboard in Applescript

Is it possible to get the name of an Illustrator artboard in Applescript?
This script works perfectly until I try to get the name of the artboard:
tell application "Adobe Illustrator"
tell document 1
set artboards_count to count of artboards
set c to 1
repeat while c <= artboards_count
log index of artboard c as text
log artboard rectangle of artboard c as text
log name of artboard c as text -- this line fails
set c to c + 1
end repeat
end tell
end tell
the line log name of artboard c as text fails - everything else works ok.
The message is:
Adobe Illustrator got an error: Can’t get name of artboard 1 of document 1. (-1728)
Any idea as to why?
Setting the name fails too, BTW. However if I do
tell application "Adobe Illustrator"
tell document 1
return properties of artboard 1
end tell
end tell
I get (carriage returns added for clarify):
artboard rectangle:0.0, 0.0, 841.889999999999, -595.280000000001,
ruler PAR:1.0, show center:false, show cross hairs:false,
show safe areas:false, ruler origin:0.0, 0.0, name:Artboard 1,
container:document 1, best type:reference, default type:reference,
class:artboard, index:1
from which one would think the property nameshould be there.
The name property is read-only so there is no way to change it once the artboard exists. And even though you can get the properties of an artboard, you can’t convert it to a string if you simply want to target a particular artboard. But there is a way to do it nonetheless that I discovered by accident. Suppose you have several artboards and want to target the artboard named "Squash this". Here's how to do that:
Tell application "Adobe Illustrator"
tell current document
set artCount to number of artboards
repeat with i from 1 to artCount
set artProp to get properties of artboard i
try
set propString to artProp as string --this will fail
on error
set errorDisp to text of result --this captures the text of the error
set errorDispText to errorDisp as string --changes the text to a ¬
searchable string
end try
if errorDispText contains "quash" then
display notification "errorDispText" --oddly enough, this displays just ¬
the artboard name
exit repeat
end if
end repeat
end tell
end tell
The best solution could be coercion; take the record returned from the Artboard properties and coerce the record into a list. So, your name property from the record becomes the 7th item in the list. The code below will get Artboard dimensions, name and index, and place them into a list for later use in your script.
Hope this helps!
tell application "Adobe Illustrator"
set artboardDetails to {}
tell front document
set allArtboards to every artboard
repeat with i from 1 to count of allArtboards
set thisArtboard to item i of allArtboards
set thisArtboardProps to properties of thisArtboard
set thisArtboardDimensions to artboard rectangle of thisArtboardProps
set thisArtboardIndex to index of thisArtboardProps
--WORKAROUND
set thisArtboardPropsCoerce to thisArtboardProps as list
set thisArtboardName to item 7 of thisArtboardPropsCoerce
set the end of artboardDetails to {thisArtboardName, thisArtboardIndex, thisArtboardDimensions}
end repeat
end tell
end tell
(*
--WORKAROUND
The following is a workaround for error returned from:
set anArtboardName to the name of anArtboard -- this line will not return a result, just errors out
BUT WE NEED TO WATCH OUT FOR THE COERCED LIST!
So, we have to coerce the properties record of artboard to a list first
WEIRD I KNOW!
The coercion then changes the record of 11 items to a list of 12 items
set anArtboardProps to properties of anArtboard as list
set anArtboardName to item 7 of anArtboardProps
NOW LETS MATCH UP THE COERCION ITEMS
The first lines are from the record properties, and the second lines are from the coerced to list versions.
01. artboard rectangle:{0.0, 768.0, 1366.0, 0.0},
01. {0.0, 768.0, 1366.0, 0.0},
02. ruler PAR:1.0,
02. 1.0,
03. show center:false,
03. false,
04. show cross hairs:false,
04. false,
05. show safe areas:false,
05. false,
06. ruler origin:{0.0, 0.0},
06. {0.0, 0.0},
07. name:"Artboard 1",
07. "Artboard 1",
08. container:document 1,
08. document 1,
09. best type:reference,
09. reference,
10. default type:reference,
10. reference,
11. index:1
11. artboard,
12. NULL - nothing in the record
12. 1 - so this line is the index of the artboard
*)
Here is my workaround. As the «class bAl9» vanishes every time you compile, you have to copy/paste every time, but it works. And yes, «class bAl9» will turn to "name" and will not compile correctly the next time. Thanks Adobe !
tell application "Adobe Illustrator"
tell document 1
repeat with x in (every artboard)
log index of x as text
log artboard rectangle of x as text
log «class bAl9» of x as text -- artboard's name property = «class bAl9»
end repeat
end tell
end tell
The name property of artboards is read/write, it's easy to rename artboards this way once you get the trick.
Edit : For the loop in a list, I always use the repeat with x in l statement. It's smart and fast.
Stu's answer was the best that I found. Coercing the record to a list is brilliant – I didn't even know you could do that. My code was:
repeat with i from 1 to the (count of AllArtBoards)
set ThisArtBoard to item i of AllArtBoards
set ThisArtBoardName to item 7 of ((properties of ThisArtBoard) as list)
set the end of AllArtBoardNames to ThisArtBoardName
end repeat

Support With Sharing AppleScriptObjC Variable To Label Object

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

Resources