I'm trying to send the spacebar key to an application without activating it first. The following code almost does what I want but it brings the application to the foreground first.
tell application "X"
activate
tell application "System Events" to key code 49
end tell
I don't think you can send a keystroke to an inactive application, but you can hide an app immediately after activating it and executing code for it. This does however, cause the app to briefly flash before it hides.
tell application "System Events"
tell application "X" to activate
key code 49
set visible of process "X" to false
end tell
Sending a keystroke can basically be seen as using a keyboard, but the only difference is that the keys that need to be pressed are already predefined. The rest of the process revolving around this doesn't change. This means that the application itself still needs to be opened and activated before you can actually send keystrokes to it.
Depending on the application however, it might be possible to use certain Applescript functions in the application's API to send different inputs to the application without having to activate it first. Take the Messages API for instance:
tell application "Messages"
set theBuddy to buddy "someone#mac.com" of service "iMessage"
send "Hi there" to theBuddy
end tell
How about deactivating it afterwards?
activate application "X"
tell application "System Events" to key code 49
activate me
Position is offscreen to prevent it from flashing where the user can see it or reduce its opacity.
There are courses of investigation.
If the application is one you have developed yourself you have the following options:
*simply have a public property exposed and set that to the key you want to send it.
*have your application polling a folder for a file and you send your instructions via that.
With a windows API hook of some type you can get control of the application without activating it. I am pretty certain if I put my mind to it I could take control of anything on the computer.
In simple terms think out of the box, it does not necessarily need to be a key press you send, you just want to instruct it to do something. There is loads of options Interface Marshalling, Interops, OLE, DDE, looks like I have turned up on this site just in time!
Related
I was able to identify the names of some elements to make the click command, but I could not find the name of the element that identifies the accounts.
The idea is to click on an account, copy the token and concatenate it with a password inside the script
Commands already used:
tell application "System Events"
tell process "Authy Desktop"
set visible to true
return every UI element of front window
end tell
end tell
tell application "System Events" to return value of every attribute of window of application process "Authy Desktop"
I have copied some scripts on the site and the result is that Outlook creates a new forwarded message but the recipient is blank. Have tried multiple forms but always the blank recipient. It's a script I will only run for messages I manually select, so I can't use a simple forwarding rule in the app.
Any ideas?
tell application "Microsoft Outlook"
repeat with eachSelectedMessage in selectedMessages
set theForwardedMessage to forward eachSelectedMessage with opening window
make new recipient at theForwardedMessage with properties {email address:{address:"recipient#somewhere.com", name:"Lumpkin Skinbark"}}
send theForwardedMessage
end repeat
end tell
New information: It turns out the recipient is being populated correctly and put in my Outbox, but not being sent. In addition, a new window pops up with the same forwarded message and a blank recipient. That new window was the one I saw that led me to believe the recipient was not populating.
My workaround for now is to periodically hit the send/receive button in Outlook to send the messages sitting in the Outbox (I don't know why they don't just get sent automatically like everything else). Then I added:
close window 1
to close the open window -- don't know why it's created and don't want it, but might as well just close it and move on.
So I do have it working, but it's not elegant.
I finally figured out how to make this work: DON'T Open the forwarded email window until AFTER you've set the email properties, especially the Recipients.
Here's my test script:
set newSubjectStr to "TEST -- DON'T OPEN at first."
set recipientEmail to "somebody#InvalidDomainName.com"
tell application "Microsoft Outlook"
set selMsg to current messages
set oMsg to item 1 of selMsg
set subjectStr to subject of oMsg
set oFwdMsg to ¬
forward oMsg without opening window ## This is KEY. Don't Open Window
make new recipient at oFwdMsg ¬
with properties {email address:{address:recipientEmail}}
set subject of oFwdMsg to newSubjectStr
--- NOW OPEN FWD EMAIL WINDOW ---
open oFwdMsg
end tell
--- SEND NOW IF NO MORE CHANGES ---
-- Works fine if I manually send, or use keyboard shortcut --
-- Send and Close Window ---
delay 0.2
tell application "Microsoft Outlook" to activate
tell application "System Events"
key code 36 using {command down} -- CMD-RETURN
end tell
--- BELOW SEND BY SCRIPT LEAVES MSG IN OUTBOX ---
--send oFwdMsg
--close window 1
--sync folder "Outbox"
NOTE to Moderators: Is there a way to get syntax highlight for AppleScript?
Alright guys, so I have a script set up to turn off the "require password on wake" function when I am at home. It pings my phone to see if I am connected to the network, and if not turns on lock to wake. So:
try
do shell script "ping -c2 X.X.X.X"
set theResult to the result
if theResult contains " 2 packets received," then
tell application "System Events"
tell security preferences
get properties
set properties to {require password to wake:false, require password to unlock:false}
end tell
end tell
end if
on error
tell application "System Events"
tell security preferences
get properties
set properties to {require password to wake:true, require password to unlock:true}
end tell
end tell
end try
end
This works just fine, however it asks to authenticate. I don't really want to use the enter text & return route, nor the clipboard route, because I don't want the password in the script... so is there a way to avoid the authentication?
If your goal is to enable/disable "password on wake" rather than to run that particular script without authentication, use either
tell application "System Events"
set require password to wake of security preferences to true
end tell
or
do shell script "defaults write com.apple.screensaver -int 1"
and the same with "to false" and "-int 0" to turn the setting off. None of these require authentication, as they're simply changing a user-level preference (stored in
~/Library/Preferences/com.apple.screensaver.plist
on my system, though this is an implementation detail you shouldn't rely on).
What triggers the authentication dialog in your script is the other property, "require password to unlock", equivalent to the "Require an administrator password to access locked preferences" option in the "Advanced..." part of Security Preferences. Under the hood, this option is equivalent to changing a number of settings in the Authorization Services database,
/private/etc/authorization
controlling whether various system-wide preferences may be left unlocked for unauthenticated changes.
System Events does appear to have a (less serious) bug, however: on my systems, setting "require password to unlock" has no effect, whether I authenticate as an admin or not.
There are two parts to this answer:
there is no way to pass the password, either via the script or via GUI Scripting, to the SecurityAgent application, which is in charge of the prompt (that is by design), nor can you suppress it altogether; this being said,
you can ignore the prompt and dismiss the window without inputting a password – your property settings will be applied even in that case (tested on OS X 10.7.4).
Reported as a Security issue to Apple as rdar://11484075
UPDATE: Apple Product Security does not consider this a security issue, but the bug itself is still tracked (I’ll have to guess, as it is closed as duplicate of another radar, which is not available on Openradar, but I’d expect the spurious dialog appearing to be the issue Apple has its eyes on).
I have an Automator Application containing the action "Show Growl Notification", which is always empty when it appears. I've tried using the "Get Specified Text" and "Get Value Of Variable" actions directly before it, but nothing I've tried seems to work.
The code sample from gadgetmo is almost correct but won't work as is with Growl 1.4
An application must be registered and enabled and must provide a list of notifications it will present and also a list of which of those notifications are enabled.
You can skip all that config stuff if you just hijack the preconfigured automator settings as shown below.
Key points to note are "Automator notification" as the name of the notification and "Automator" as the source application.
tell application "System Events"
set isRunning to (count of (every process whose bundle identifier is "com.Growl.GrowlHelperApp")) > 0
end tell
if isRunning then
tell application id "com.Growl.GrowlHelperApp"
notify with name ¬
"Automator notification" title ¬
"Processing Files" description ¬
input as text application name "Automator"
end tell
end if
Use this in a Run Applescript:
tell application "System Events"
set isRunning to (count of (every process whose bundle identifier is "com.Growl.GrowlHelperApp")) > 0
end tell
if isRunning then
tell application id "com.Growl.GrowlHelperApp"
notify with name ¬
"Test Notification" title ¬
"Test Notification" description ¬
"This is a test AppleScript notification." application name "Growl AppleScript Sample"
end tell
end if
Or download it here.
You can also use this with input.
The action just passes the input through, and doesn't have any text fields that accept variables, so you will need to manually put in the message you want. The Show Growl Notification action (located inside the GrowlHelperApp bundle) does use an AppleScript, so you could modify a copy to do something like use the input if the message field is blank.
I have a Mail rule set up to launch the following applescript:
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
-- do stuff, including...
CheckAddressBook(theName, theAddress)
end tell
end perform mail action with messages
end using terms from
on CheckAddressBook(theName, theAddress)
tell application "Address Book"
-- do stuff
end tell
end CheckAddressBook
Whenever this mail rule executes, it launches address book. Its not activated, but it suddenly shows up on my desktop. My question is, can tell blocks be instructed to launch the application silently, and quit when complete?
AppleScript can't control an application without it running. That's just the way it works. There are other methods you might use to access the Address Book database without launching the application, but if you're using AppleScript to get data from your Address Book database the application has to launch. My recommendation would be to simply add a quit command as suggested by Fábio.
To read the Address Book Database without launching "Address Book.app" I´d suggest to have a look at the command line tool "contacts" available for free here. You would then run it from Applescript like do shell script "/usr/bin/contacts Peter" and handle the values returned.