Del.icio.us Digg Yahoo. SlashDot Twitter FaceBook Google. Propeller. StumbleUpon Windows Live

AppleScript

Programming Languages -> AppleScript

AppleScript programming and task automation, Automator.

Your name or nick:
Your email:
Subject:
Message:
  - no <, >, [ or ] tags will go through except for <code> and </code> wich will draw submitted programs code. URLs will be converted to link


Total messages: 4

How to use FTP via AppleScript on Mac
posted by: Macuser on Wednesday, October 1, 2008 at 8:50 PM
Built-in UNIX FTP is a powerful tool if you need to update your website or exchange files between computers.
In my case I need to update my websites daily and instead of doing it manually - I wrote a small AppleScript that does it automatically (triggered by the same Perl daemon).
Here is the script:

tell application "System Events"
-- check if Terminal has been activated before for clean exit after program done its work
if not (exists process "Terminal") then
set aDone to 0
set wCounter to 0
tell application "Terminal"
activate
make new document
end tell
else
set wCounter to 1
tell application "Terminal"
activate
make new document
end tell
end if

delay 2
end tell

tell application "System Events"
tell process "Terminal"
if wCounter > 0 then
keystroke "n" using {command down} -- open new terminal window
delay 3
end if
keystroke "k" using {command down} -- clean the screen
delay 1
keystroke "ftp" -- start FTP
delay 1
keystroke return
delay 5
keystroke "open 127.0.0.1" -- connect to server
delay 1
keystroke return
delay 10
keystroke "username"
delay 1
keystroke return
delay 2
keystroke "password"
delay 1
keystroke return
delay 2
keystroke "cd your/site" -- remote "cd"
delay 1
keystroke return
delay 2
keystroke "lcd from/folde" -- local "cd"
delay 1
keystroke return
delay 2
keystroke "k" using {command down} -- clean screen
delay 1
keystroke "mput *.txt" -- upload all TXT files
delay 1
keystroke return
delay 1
keystroke "p" -- make FTP upload all of them
delay 1
keystroke return
delay 8
-- check that upload was finished
repeat with i from 1 to 80
keystroke "a" using {command down}
delay 1
keystroke "c" using {command down}
delay 1
set screenData to (the clipboard) as text
if screenData contains "complete" then
exit repeat
end if
delay 3
end repeat
delay 1
-- now, if Terminal was active - just close the window, else quit Terminal
if wCounter > 0 then
keystroke "w" using {command down}
delay 1
else
keystroke "q" using {command down}
delay 1
end if
end tell
end tell


Once again, "delay" amount depends on speed of your computer and connection speed.
How to check if page is already loaded in Safari
posted by: Macuser on Wednesday, October 1, 2008 at 3:38 PM
This is a portion of an Applescript that checks if page is already loaded in Safari:

tell application "Safari"
delay 2
set URL of document 1 to "http://www.google.com/"
set myURL to URL of document 1
delay 5
set web_page_is_loaded to false
set myCounter to 0
set maxCounter to 30
set my_delay to 1
repeat until web_page_is_loaded is true
if name of window 1 contains "Loading" or name of window 1 contains "Untitled" then
delay my_delay
else
set web_page_is_loaded to true
end if
set myCounter to myCounter + 1
if myCounter is maxCounter then
set web_page_is_loaded to true
end if
end repeat
end tell


Please keep in-mind that "delay" depends on your computer and Internet connection speeds and may require different values.
Turn on/off Java and Javascript in Safari
posted by: Macuser on Tuesday, September 30, 2008 at 10:46 PM
Another snippet of the program that turn of and on Java and JavaScript in Safari.
Why would one do that? When you tell Safari to browse pages^ if there is no need for Java and such - pages load much faster.
Page load status could be detected without Java turned on and if you need just a source of the page it is the answer to many issues associated with Java-rich pages (not much they could do after that with unattended browser).

Turn Java and Javascript ON


tell application "Safari" to activate
delay 2
tell application "System Events"
tell application process "Safari"

-- open Preferences window
keystroke "," using {command down}
-- window title varies according to type of Prefs initially
delay 2
-- selected, so save to a variable
get value of static text 1 of window 1
copy the result as string to varWindowName

-- switch to Security Prefs
click button "Security" of tool bar 1 of window varWindowName

-- toggles JavaScript checkbox
--click checkbox "Enable Java" of group 1 of group 1 of window "Security"
delay 2
-- get new state for confirmation dialog
get value of checkbox "Enable Java" of group 1 of group 1 of window "Security"
copy the result as number to varResult
if varResult = 0 then
--set varResult to "ON"
click checkbox "Enable Java" of group 1 of group 1 of window "Security"
end if

--click checkbox "Enable JavaScript" of group 1 of group 1 of window "Security"

get value of checkbox "Enable Javascript" of group 1 of group 1 of window "Security"
copy the result as number to varOption
if varOption = 0 then
--set varOption to "ON"
click checkbox "Enable JavaScript" of group 1 of group 1 of window "Security"
end if
-- close Preferences window
click button 1 of window "Security"
end tell
end tell


Turn Java and Javascript Off


tell application "Safari" to activate
delay 2
tell application "System Events"
tell application process "Safari"

-- open Preferences window
keystroke "," using {command down}
-- window title varies according to type of Prefs initially
delay 2
-- selected, so save to a variable
get value of static text 1 of window 1
copy the result as string to varWindowName

-- switch to Security Prefs
click button "Security" of tool bar 1 of window varWindowName

-- toggles JavaScript checkbox
--click checkbox "Enable Java" of group 1 of group 1 of window "Security"
delay 2
-- get new state for confirmation dialog
get value of checkbox "Enable Java" of group 1 of group 1 of window "Security"
copy the result as number to varResult
if varResult = 1 then
click checkbox "Enable Java" of group 1 of group 1 of window "Security"
--set varResult to "OFF"
end if

--click checkbox "Enable JavaScript" of group 1 of group 1 of window "Security"

get value of checkbox "Enable Javascript" of group 1 of group 1 of window "Security"
copy the result as number to varOption
if varOption = 1 then
--set varOption to "OFF"
click checkbox "Enable JavaScript" of group 1 of group 1 of window "Security"
end if
-- close Preferences window
click button 1 of window "Security"
end tell
end tell
Phidget Servo VIA Applescript
posted by: Macuser on Tuesday, September 30, 2008 at 9:28 PM
This is simple and very robust alternative to Indigo and X10 solutions.
Phidget 4 Servo controller will run for about $80 and servos could be picked-up for $12-15 each.
I hooked-up appliances switches to the servos and wrote a small Perl program that run by the daemon. Perl in-turn runs Applescripts for each servo (or for all of them).
Shippet of the program that drives one of the servos:

tell application "Phidget Servo Controller" to activate
tell application "System Events"
-- Off switch position
tell process "Phidget Servo Controller" to tell slider 1 of list 1 of window 1 to set value to 145
delay 1
keystroke space
delay 1
-- Newtral switch position
tell process "Phidget Servo Controller" to tell slider 1 of list 1 of window 1 to set value to 80
delay 1
keystroke space
delay 1
end tell
tell application "Phidget Servo Controller" to quit


"keystroke space" needed to make servo actually move to the given position.

Perl Program is very simple:

#!/usr/bin/perl
`osascript /turnoff.scpt`; sleep(3);
exit(0);


and Perl called when some event comes to play.

 

Home Articles Discussion Contact Link to

More links: Rawhack.Com