AppleScript NSWorkspaceDidLaunchApplicationNotification don't get event - applescript

I am new to applescript, This simple AppleScript must wait until any application lunch and then display notification for test, but nothing happened!
any solution ?
thanks
script Autopilot
property NSWorkspace : class "NSWorkspace"
on run
tell (NSWorkspace's sharedWorkspace())'s notificationCenter() to ¬
addObserver:me selector:"notificationDelegate:" |name|:"NSWorkspaceDidLaunchApplicationNotification" object:(missing value)
end run
on notificationDelegate:sender
display notification "notify"
end notificationDelegate:
end script

Try this
property NSWorkspace : class "NSWorkspace" of current application

Related

How to convert window object to string in AppleScript?

I've an AppleScript that retrieves window id of an app.
Example following script retrieves the window id of Brave Browser.
set urls to {"https://google.com"}
tell application "Brave Browser"
set myNewWindow to make new window
repeat with theURL in urls
tell myNewWindow to open location theURL
end repeat
delay 0.3
log myNewWindow
return class of myNewWindow //comment - returns "window" as a class
end tell
My goal is it possible to convert the window id to a string and vice-versa.
Why conversion?
I want to save window id in UserDefaults on macOS.
Note: This AppleScript is used in macOS app.
To be honest, it's hard for me to understand why the ID should be kept in defaults. Because theID variable will already store this value throughout the script. And at any time you can close the window using a variable in a later part of the script. I can only assume one thing: you want to open a window locally and then close it from other script executed on a remote machine.
And getting the window ID in Brave Browser as text is easy:
set urls to {"https://google.com"}
tell application "Brave Browser"
set myNewWindow to make new window
repeat with theURL in urls
open location theURL
end repeat
delay 0.3
set theID to (id of myNewWindow) as text --> the ID of window in text form
end tell
-- write theID value to defaults here
Closing the window from other script:
-- first, read theID from defaults here
tell application "Brave Browser"
close window id (theID as integer)
end tell

Applescript quit after 1 minute

i have a very simple applescript app in xcode that just opens a window and has a button that redirects to a website. Is there a way to automatically exit the window if the user hasnt clicked the button in 60 seconds.
Here is my code
script AppDelegate
-- property parent : class "NSObject"
-- IBOutlets
property theWindow : missing value
on applicationWillFinishLaunching_(aNotification)
-- Insert code here to initialize your application before any files are opened
end applicationWillFinishLaunching_
on buttonClicked_(sender)
open location "https://example.com"
quit
end buttonClicked_
on applicationShouldTerminate_(sender)
-- Insert code here to do any housekeeping before your application quits
return current application's NSTerminateNow
end applicationShouldTerminate_
end script
Since the window is being shown when the application starts up, you can just call a method/handler after a delay from the applicationWillFinishLaunching method. Since you are also quitting from the button handler, that can perform double duty, for example, replace these handlers:
on applicationWillFinishLaunching_(aNotification)
my performSelector:"buttonCLicked:" withObject:me afterDelay:60
end applicationWillFinishLaunching_
on buttonClicked_(sender)
if sender is not me then open location "https://example.com"
current application's NSApp's terminate:me -- quit
end buttonClicked_

How do I create calender alarms using Applescript (using OSX Yosemite/Mavericks)?

I have an Applescript here that triggers when an ICS file has been dropped into a folder. All it does it imports the ICS file into the Calendar and then create alarms for each calendar entry.
This was working fine on Mountain Lion. But hasn't been working since Mavericks/Yosemite. The calendar entries are being imported. But no alarms are being created.
This is code I use to add the alarm:
tell application "Calendar"
tell calendar "Home"
delay 1
set myevents to every event
repeat with oneevent in myevents
tell oneevent
set mysummary to summary
display dialog mysummary
make new display alarm at end with properties {trigger interval:-30}
end tell
end repeat
end tell
end tell
As you can see from the code, I am displaying the summary for the event just as a test. I just want to find out it's opening the correct event.
I've looked/searched for examples/documentation on the topic but hardly find anything recent (i.e. Mavericks and above).
Any ideas on how to fix my problem? Thanks a lot
try this
tell application "Calendar"
tell calendar "Home"
repeat with oneevent in (get every event)
tell oneevent
set startDate to start date
set newAlarm to make new display alarm at end of display alarms with properties {trigger date:startDate}
set trigger interval of newAlarm to -30
end tell
end repeat
end tell
end tell

Applescript update iTunes stream title without obstructing rest of script

I am using the code below to get the current iTunes stream title. That works great. Just like I need it to be, the script updates every time when the iTunes stream title changes, the variable Title updates itself as well.
repeat
tell application "iTunes"
set Title to current stream title
end tell
delay 2
end repeat
However, because of the ongoing "repeat" function, the rest of the script will not execute. Do you know of any way to still get the variable Title to be updated when iTunes changes its title, but not obstruct the rest of the script? As if it sort of runs in the background? Thank you!
A solution, use an handler (Handlers are also known as functions, subroutines, or methods).
Put the rest of the script in an handler, call this handler from the loop.
Like this :
set oldTitle to ""
repeat
set t to ""
try
tell application "iTunes" to set t to current stream title
end try
if t is not "" and t is not oldTitle then -- the title changed
set oldTitle to t
my doSomething(t) -- call the handler
end if
delay 3
end repeat
on doSomething(Title)
-- put the rest of the script here
end doSomething

Applescript for creating New Message with Mail application

I have an AppleScript for Mail.app which opens a new message window with pre-defined recipient address and subject. This script opens a new window every time I run it:
tell application "Mail"
set newMessage to make new outgoing message with properties {subject:"some subject", content:"" & return & return}
tell newMessage
set visible to true
make new to recipient at end of to recipients with properties {name:"some name", address:"some address"}
end tell
activate
end tell
But I want the script to open a new message window only when the previously opened window is closed – otherwise, the previously opened window should come to the front.
Can anyone please help me in modifying this script to achieve the above mentioned functionality?
I didn't test this but it should do what you need... at least it shows you the proper approach. You basically use a "property" to keep track of some value from the last time the script was run. In this case we check for the name of the frontmost window and see if it matches your criteria. If the window name doesn't do what you need then just find some other value to track between launches of the script. The basic approach should work.
EDIT: using the ID of the message, which is unique, the following will do what you want:
property lastWindowID : missing value
tell application "Mail"
set windowIDs to id of windows
if windowIDs does not contain lastWindowID then
set newMessage to make new outgoing message with properties {subject:"some subject", content:"" & return & return}
tell newMessage
set visible to true
make new to recipient at end of to recipients with properties {name:"some name", address:"some address"}
end tell
activate
set lastWindowID to id of window 1
else
tell window id lastWindowID
set visible to false
set visible to true
end tell
activate
end if
end tell
the visibility toggle seems to be the only way to get the window in front, as frontmost is a read-only property. The lastWindowID property will store the ID as long as the script is not re-compiled (caveat empteor: do not put this into an Automator service, as these get re-compiled every time the service is loaded).

Resources