This is the first script I’ve worked on and I received help from a variety of people, some of which from these boards, so thanks go out to them.
This script is designed to connect to a given website (currently set to NASA’s astronomy picture of the day), download the image to the desktop and set it as the desktop image. It works most of the time, I hope it works for you guys.
Craig
set the destination_file to ((path to desktop as string) & "space.jpg") as file specification
tell application "Safari"
activate
--open location target_URL
make new document at end of documents
set URL of document 1 to "http://antwrp.gsfc.nasa.gov/apod/"
delay 2
set done to false
repeat with i from 1 to 20
tell application "Safari"
if (do JavaScript "document.readyState" in document 1) is "complete" then
set done to true
else
delay 1
end if
end tell
end repeat
if done is true then
set thisImageSRC to do JavaScript the "document.images[0].src" in document 1
end if
end tell
tell application "URL Access Scripting"
download thisImageSRC to destination_file replacing yes
end tell
tell application "Finder"
set desktop picture to file "space.jpg" of desktop
end tell
I would also like to submit a similar script to Craig’s, however this one periodically downloads a webcam photo from Lick observatory, altenately showing a view westward to San Jose, and eastward into the Diablo mountain range (with telescopes in foreground). The script itself must be saved as an application with the ‘stay open’ and ‘run only’ checkboxes checked. This allows the script, using AppleScript’s ‘idle’ command, to run continuously in the background to download the real-time photos. You can change the frequency of the photo download by changing the ‘return’ property which is currently set at 90 seconds. Note that if you wish to modify the script, be sure to save the script file as a script without the ‘run only’ boxed check; otherwise you will not be able to open the script. Finally, you will notice that the script uses Unix’s ‘curl’ function to download the website photo (I incorporate this from another post on Macscripter). The nice thing about this is that curl does not require additional code to detect that the download is finished; it does this as part of its own functionality. Finally, I was unable to download the same photo subsequently because the desktop picture won’t toggle if the downloaded image name is the same. My work-around was to download the two different photos alternately. Here’s the script:
property i : 1
on idle
alternatePics()
return 90
end idle
on alternatePics()
if i = 1 then --alternate pictures
set i to 2
else
set i to 1
end if
set targetImage to "Cam" & i & ".ts.JPG"
(* The url to get the image from *)
set theImageUrl to "http://mthamilton.ucolick.org/hamcam/" & targetImage
set destinationPath to ((path to application support as string) & targetImage) as file specification
(* The name to save the image as *)
set ODel to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
set theImageName to (text item -1 of (theImageUrl as string) as list) as string
set AppleScript's text item delimiters to ODel
(* Where to save the image *)
set desktopPath to (path to application support) as string
set targetPath to (POSIX path of ((desktopPath & theImageName) as string))
set targetPath to ("'" & targetPath & "'") as string --> In case a space gets in there
(* Get the image using curl *)
try
set theCurlCommand to ("curl -o " & targetPath & " " & theImageUrl)
do shell script theCurlCommand
--set DLStatus to "File downloaded successfully!"
on error
set DLStatus to "File could not be downloaded!"
end try
(* Display the status *)
--display dialog DLStatus
tell application "Finder"
--beep
set desktop picture to destinationPath
end tell
end alternatePics
Model: Macbook
AppleScript: 2.1.1
Browser: Safari 419.3
Operating System: Mac OS X (10.4)
One last note: to turn the continously running script application off, you must use ‘force quit’—CTRL+Apple + esc
There shouldn’t be any technical reason preventing you from saving this as an application that is not run-only.
Scripts saved as applications should appear in the dock and have a title bar (unless you’ve modifed them not to).
Thanks for the correction Philip. You learn something new all the time with AppleScript.
Also, to make the application run automatically at startup, you can drag the application icon to the ‘Login Items’ list, within System Preferences/Accounts.
A final suggestion: I was thinking of the possibility of an AppleScript that would automatically take scheduled screenshots of sunrise and sunsets or perhaps hourly interval screenshots that would be saved to the screen saver folder. Perhaps someone out there knows how.
Thanks,
Antony
If you try the script–saving it as a regular application-- it “download(s) it ever few minutes to a folder” as you suggest. What I don’t see is how to make the system preferences (the screen-saver options?) specify periodic loading from a folder. I found it necessary to automate all this within the script. Perhaps you know something I don’t. I did find that I could set the screen saver to perodically view the collection of screen shots I collected into a folder. This makes a wonderful screen saver.
Just trimmed the script down a bit, have not change any functions.
there seem no need to use the Applescript delimiters, since you had already defined the name in targetImage
There where also other parts that need not be repeated.
property i : 1
on idle
alternatePics()
return 90
end idle
on alternatePics()
if i = 1 then --alternate pictures
set i to 2
else
set i to 1
end if
set targetImage to "Cam" & i & ".ts.JPG"
(* The url to get the image from *)
set theImageUrl to "http://mthamilton.ucolick.org/hamcam/" & targetImage
set destinationPath to ((path to application support as string) & targetImage) as file specification
(* Get the image using curl *)
try
set theCurlCommand to ("curl -o " & quoted form of (POSIX path of ((destinationPath) as string)) & " " & theImageUrl)
do shell script theCurlCommand
--set DLStatus to "File downloaded successfully!"
on error
set DLStatus to "File could not be downloaded!"
end try
(* Display the status *)
--display dialog DLStatus
tell application "Finder"
beep
set desktop picture to destinationPath
end tell
end alternatePics
Thanks for the edits. Parts of the script were copied from elsewhere without scrutiny. I should also suggest that you remove the ‘on error’ procedure that displays the error message. Otherwise you will be working on your computer one day and a very puzzling message will present itself–a message that serves no good use as the script will most certainly download the next photo update successfully.
Antony