Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Triggering applescript on window focus change

I'm writing a script to set my ichat status to the current Safari page I'm browsing and here's what I have so far:

tell application "Safari"
activate
set my_URL to the URL in document 1
end tell
tell application "iChat"
set the status message to my_URL
end tell

How can I run this so that whenever I change Safari windows or tabs, it'll trigger the script and change my iChat status on a consistent basis?

MBP 2ghz, Mac OS X (10.5.2)

Posted on Apr 24, 2008 11:40 AM

Reply
3 replies

Apr 24, 2008 12:48 PM in response to formasfunction

You can't.

The best you can do is have the script run repeatedly, say every 10 seconds, checking the current URL and updating iChat if it's changed.
There is no way to automatically run the script as the window changes in Safari - that would require a change in Safari to call the script since it's the only thing that knows it's changed.

It's relatively easy to rewrite your script to check periodically. Either wrap it in a 'repeat/end repeat' block (with a 'delay 10' (or however many seconds you want) for the interval between checks).

A slightly better solution involves using an idle handler. This puts less load on the system than a 'delay', but needs a little more work.
First you'll need to put your code into a handler (which is easy enough), then you call that handler via the on idle handler. Save the script as a 'stay open application' and you're done.

on checkURL()
tell application "Safari" to set my_URL to URL of document 1 -- no need/desire to 'activate'
tell application "iChat" to set status message to myURL
end checkURL

on idle
my checkURL()
return 10 -- or however many seconds before the next pass
end idle

Apr 24, 2008 3:37 PM in response to formasfunction

Here's what I found:

on getFrontApp()
set front_app to (path to frontmost application as Unicode text)
set AppleScript's text item delimiters to ":"
set front_app to front_app's text items
set AppleScript's text item delimiters to {""} --> restore delimiters to default value
set item_num to (count of front_app) - 1
-- display dialog item_num
-- display dialog item item_num of front_app
set app_name to item item_num of front_app
set AppleScript's text item delimiters to "."
set app_name to app_name's text items
set AppleScript's text item delimiters to {""} --> restore delimiters to default value
return item 1 of app_name
end getFrontApp

Triggering applescript on window focus change

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.