Can an applescript "tell" call execute without visibly launching the application? - applescript

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.

Related

Mac Automator/Applescript(?) to extract URL's from Mail messages

I'm trying to get a text document list of any links in a bunch of email messages that reside in the latest Mac Mail.app (OS X 10.10.2 Yosemite), and am simply stumped.
I thought I'd be able to just...
Put a couple of Automator.app actions together in a Service/Workflow,
Select/highlight all the email messages within the Mail.app,
Select that Service or run that Workflow,
And have a text document with every URL/link that could be found within them all.
However, this didn't work.
I figured out how to do this with one email message at a time, but that's not what I want. attached is a screenshot of 3 workflows. The first one is the one that works with just one email message & highlighting all the text in it & running the Service. the other two just simply don't work.
I also notice that the the first shows up in the Service Menu with a single email open; once I highlight more than one email message, the option goes away from the Service menu.
Any tips or tricks?
I figured out how you could reach your goal, start with creating a new service within Automator (input: "No input", application: "Mail")
The first action is Get Selected Mail Messages (Get selected: messages)
The second action is Execute AppleScript with the following script:
on run {input, parameters}
set mailContentList to {}
tell application "Mail"
repeat with selectedMail in input
set end of mailContentList to content of selectedMail
end repeat
end tell
return mailContentList
end run
This script simply walks through the given messages, reads out the content and passes this list to the next action
The third action is Extract URLs from Text. This is listed as "Extract Data from Text" and one of the types of data is "URLs".
And the final action is New TextEdit Document
Save it with a nice name like Extract URLs from selected mails
After that the Service is available inside the Services menu inside the Mail app.
In my test I found a few internal URLs without http:// from links to anchors, so maybe you want to delete all URLs that do not start with http. You can do so by using another action before creating the new TextEdit document:
Filter Paragraphs with options "Paragraphs that start with http" (don't know how these parameters are called in English Automator, sorry)
Feel good, Michael / Hamburg

make applescript run in the background

I have tried using the script in Outlook 2011: Adding some messages to "Waiting for reply" folder. Basically, when I compose an Outlook message and hit Ctrl-Shift-W (as specified in the script name), it tries to send the message, find it in the sent folder and move it to a "waiting" folder. To make sure Outlook has a chance to send the message, it tries to delay before finding the message in the sent folder, repeatedly.
Unfortunately, the delay doesn't work (it doesn't delay), making it fail. Checking to do shell script "sleep 1s" means that Outlook is then stuck and doesn't send the message until the script fails.
I figured that outlook is waiting for the script to end before doing any background tasks. So the script needs to run in the background. Maybe launching an auxiliary script or something similar.
The problem is I don't know how to do this. Hence this question...
While AppleScript does have its own sleep method (delay), what you are probably looking for is the idle handler. If you save your script as an application and check “Stay open after run handler”, Mac OS X will call your script’s idle handler immediately after your “run” handler finishes. Your idle handler then returns the number of seconds until the next time it should be called; this can happen indefinitely. In your case, it might look something like this:
global messageID
on run
-- send message
-- set messageID to whatever you are using as the just-sent message’s identifier
end run
on idle
--check whether message exists in the sent folder
--note that this is just pseudocode, you’ll need to change it for your purposes
if exists messageID in sent folder then
--do something with it
quit
else
--wait another second
return 1
end if
end idle
This will repeat the idle every second until the criteria in the if is met at which point it will perform the task and quit the script.
You can try something like this:
ignoring application responses
tell application "Microsoft Outlook"
--send code goes here
sync
end tell
end ignoring
delay 5
tell application "Microsoft Outlook"
-- Find code goes here...
end tell

AppleScript - Change prefs when home

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).

Send a key code to an application without activating it first?

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!

Automator / AppleScript to process incoming emails in Mac Mail

I'm designing an app that allows users to email me crash reports if my app ever crashes. I'd like to leave Mac Mail running on a computer and when an email comes through, an automator script / AppleScript runs to process the contents of the body of the email.
I've got the entire parsing/processing done in a python script, except I have to manually copy the contents of the email into a file and then run my parser on that file.
What's the best way to set this up so I can the contents of the email be pushed into my parsing script?
Many thanks!
Probably the simplest approach is to define a Mail.app Rule. You can set up filtering conditions to specify the set of incoming email to apply the rule to and among the rule actions you can specify is one to run an AppleScript on incoming messages. Rules are managed with Mail.app Preferences -> Rules. Apple supplies examples of Rule Action scripts with Mac OS X. Look in /Library/Scripts/Mail Scripts/Rule Actions or search the web.
Here's a script that extracts from email into a file using a mail rule: MacScripter / Mail rule script for message export. Might be good for sample code for what you're doing.
Use the Dictionary in Applescript Editor to see the properties of mail and you'll quickly be able to see the properties of any mail message. Here's a quick and dirty example of getting the content of a mail message.
tell application "Mail"
set the_messages to selection
repeat with this_message in the_messages
set mytext to content of this_message
end repeat
end tell
Modify the script linked to above that copies output to a temporary file and then pass that file to your Python script to act on.

Resources