I have the following applescript. I have it set to run in ichat when i recieve an IM. It notifies me via growl of new messages:
on notify_growl(theName, theTitle, theDescription, theImage)
display dialog theImage
tell application "Growl"
notify with name theName title theTitle description theDescription application name "iChat" image theImage
end tell
end notify_growl
using terms from application "iChat"
-- register the app with growl
tell application "Growl"
set the allNotificationsList to {"Message Received"}
set the enabledNotificationsList to {"Message Received"}
register as application "iChat" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "iChat"
end tell
-- handle the iChat events
on message received theMessage from theBuddy for theChat
if ((application "iChat" is not frontmost) or (status of application "iChat" is equal to away)) then
notify_growl("Message Received", name of theBuddy, theMessage, image of theBuddy)
end if
end message received
on received text invitation theMessage from theBuddy for theChat
accept theChat
if ((application "iChat" is not frontmost) or (status of application "iChat" is equal to away)) then
notify_growl("Message Received", name of theBuddy, theMessage, image of theBuddy)
end if
end received text invitation
end using terms from
The issue is that if my buddy doesn't have an image associated, I get an error. So I want to add an if statement in notify_growl where if theImage is blank, or null, or whatever, to growl sans the image. The display dialog theImage, when empty shows a dialog that says "msng"
Test against missing value, AppleScript's analogue of a null/undefined value. In your case, that would look something like the following if you want to just leave out the image:
on notify_growl(theName, theTitle, theDescription, theImage)
tell application "Growl"
if theImage is missing value then
notify with name theName title theTitle description theDescription ¬
application name "iChat"
else
notify with name theName title theTitle description theDescription ¬
application name "iChat" image theImage
end if
end tell
end notify_growl
If you instead had a default image, you could write
on notify_growl(theName, theTitle, theDescription, theImage)
tell application "Growl"
if theImage is missing value then set theImage to defaultImage
notify with name theName title theTitle description theDescription ¬
application name "iChat" image theImage
end tell
end notify_growl
For a default image, you could do something like this:
set defaultImageFile to open for access POSIX file "/some/file/path"
set defaultImage to read defaultImageFile as "JPEG"
close defaultImageFile
There may be an easier way, but this will get you an image. However, it's sufficiently slow that it might not work in your case.
Try this ;)
on notify_growl(theName, theTitle, theDescription, theImage)
tell application "Growl"
try
notify with name theName title theTitle description theDescription application name "iChat" image theImage
on error
notify with name theName title theTitle description theDescription application name "iChat"
end try
end tell
end notify_growl
Related
When I get the reference to a window using for example:
tell application "Safari"
set theWindow to first window
end tell
log(process(theWindow)) -- # Safari
tell application "Terminal"
set theWindow to first window
end tell
log(process(theWindow)) -- # Terminal
to process(theWindow)
-- How to get app name from "theWindow"
end process
From the variable theWindow, is it possible to extract the app name "Safari"? I need to know this information from a handler because the handler can accept window reference of other application as well.
tell application "Safari"
set theWindow to first window
end tell
log (process(theWindow))
tell application "Terminal"
set theWindow to first window
end tell
log (process(theWindow)) -- # Terminal
to process(theWindow)
try
theWindow as text
on error errorMessage
end try
set ATID to AppleScript's text item delimiters
set AppleScript's text item delimiters to "\""
set appName to text item 2 of errorMessage
set AppleScript's text item delimiters to ATID
return appName
end process
I am trying to get a dialogue box in mac using apple script.
tell application "System Events"
activate
display dialog "Enter your name: " default answer "" buttons {"OK"} default button "OK" with title "Good Name"
set the Name to text returned of the result
The problem i am facing is, when i don't enter name, the popup is closing by itself throwing error. But i want it to stay alive till user gives input
You'll want to add a repeat that which acts on the error:
set theMessage to ""
set theIcon to note
tell application "System Events"
activate
repeat
display dialog theMessage & "Enter your name: " default answer "" buttons {"Cancel", "OK"} default button "OK" cancel button "Cancel" with title "Good Name" with icon theIcon
set the theName to text returned of the result
try
if theName = "" then error
exit repeat
on error
set theMessage to "Invalid. "
set theIcon to caution
end try
end repeat
display dialog "Your name is " & theName & "." buttons {"OK"} default button "OK" with icon note
end tell
When input is "" within the repeat on error is triggered, which gives an invalid message and repeats the input dialog — otherwise the script continues (name output added as example).
So I have a Hazel rule that says "If I see a statement from this company, do X" in this case I want X to do the following.
Open Evernote
Check to see if I have a note with the same title as the downloaded file's title.
If no, create a note from that file. If yes, do nothing.
So what I wrote so far is.
global fileNamez
tell application "Finder"
set fileNamez to name of theFile
tell application "Evernote"
activate
delay (3)
set searchString to "\"" & fileNamez & "\""
set matches to find notes searchString
if (not (matches exists)) then
display dialog "no matches"
create note title fileNamez from file theFile
end if
end tell
end tell
The problem is the searching, it does not work, and I don't know whats wrong with it. Anyone have any ideas?
Try:
tell application "Finder" to set fileNamez to name of theFile
if application "Evernote" is not running then
launch application "Evernote"
delay 3
end if
tell application "Evernote" to set matches to find notes fileNamez
if matches = {} then
tell application "Evernote" to set resultNote to create note from file theFile title fileNamez
tell application "SystemUIServer" to display dialog "no matches" buttons {"OK"}
end if
How do you check if a person has got an avatar? How do you unset it? How do you load an avatar from a file? From web, Facebook, gravatar? What image types can AppleScript read? Is it possible to copy an existing avatar to clipboard with AppleScript?
EDIT
OK, I know how to check if a person has got an avatar
on run
using terms from application "Address Book"
my hasImage(person (random number from 1 to length of (get people of application "Address Book") of application "Address Book") of application "Address Book")
end using terms from
end run
on hasImage(myPerson)
using terms from application "Address Book"
try
set garbage to image of myPerson
garbage
true
on error number -2753
false
end try
end using terms from
end hasImage
> How do you check if a person has got an avatar?
You shouldn't need an error handler. Just use image of [somebody] is missing value, where [somebody] is a specifier for an Address Book person.
tell application "Address Book"
image of some person is missing value
end tell
Usage:
tell application "Address Book"
set somebody to some person
if image of somebody is missing value then
display dialog name of person & " doesn't have an image."
else
display dialog name of person & " has an image."
end if
end tell
> How do you unset it?
To remove a person's image, set it to missing value.
> How do you load an avatar from a file?
To load an image from a file, read it as TIFF data:
tell application "System Events"
set imgfile to file "AENewman.jpg" of folder "friends" of pictures folder
end tell
set imgfd to open for access imgfile
set img to read imgfd as "TIFF"
tell application "Address Book"
set myFriend to item 1 of ¬
(people whose first name is "Alfred" and last name is "Newman")
set (image of myFriend) to img
end tell
close access imgfd
> From web, Facebook, gravatar?
You can use URL Access Scripting (in /System/Library/ScriptingAdditions/URL Access Scripting.app, should you wish to view the scripting dictionary) to download an image to a file, then load it as above.
on setABImage for somebody from |url|
tell application "System Events" to set tempFile to ¬
make new file at end of temporary items folder of user domain
set tempPath to path of tempFile
tell application "URL Access Scripting" to download |url| ¬
to tempPath replacing yes
set imgfd to open for access tempFile
tell application "Address Book" to set (image of somebody) ¬
to (read imgfd as "TIFF")
close access imgfd
tell application "System Events" to delete tempFile
end setABImageURL
> What image types can AppleScript read?
AppleScript's Image Events can read PICT, Photoshop, BMP, QuickTime Image, GIF, JPEG, MacPaint, JPEG2, SGI, PSD, TGA, Text, PDF, PNG, and TIFF formats. I expect the read command supports the same. Note that image types in a read command are specified as OSTypes. Address Book only supports TIFF.
> Is it possible to copy an existing avatar to clipboard with AppleScript?
You can set the clipboard to just about anything using set the clipboard to, though you might need an as clause to set the clipboard to the contents rather than a reference.
tell application "Address Book" to set the clipboard to ¬
the image of item 1 of (person whose name is "Alfred E Newman") as data
How would I quit all running user applications using Applescript?
It's okay... I think I found my answer:
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
After some googling, I found a better approach:
It uses background only to build the initial app list, rather than
visible is true. The difference is that the other scripts will fail
to quit an app that's been hidden with ⌘H.
It provides an exclusions
list so that, for example, you can prevent your script editor from
quitting each time you test the script.
Adapted from a thread on MacScripter.
-- get list of open apps
tell application "System Events"
set allApps to displayed name of (every process whose background only is false) as list
end tell
-- leave some apps open
set exclusions to {"AppleScript Editor", "Automator", "Finder", "LaunchBar"}
-- quit each app
repeat with thisApp in allApps
set thisApp to thisApp as text
if thisApp is not in exclusions then
tell application thisApp to quit
end if
end repeat
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
tell application "System Events" to set quitapps to name of every application process whose visible is true and name is not "Finder"
repeat with closeall in quitapps
quit application closeall
end repeat