PDA

View Full Version : Applescript NOOB




Revolume
Nov 19, 2010, 10:54 PM
I have never used AppleScript before, and I wanted to create a view bot (keeps refreshing webpage for views). I found one online that works perfectly, but if I want to change the URL, I have to open Applescript. The same goes for changing the refresh time (currently at every 2 seconds). Can I make it so that when the application opens, there is a simple dialog box with texts fields for both URL and refresh time? Here is what I am using now.

on run
do_the_thing()
end run

on idle
do_the_thing()
return 2
end idle

to do_the_thing()
tell application "Safari"
launch
open location "http://forums.macrumors.com/"
end tell
end do_the_thing

How can I do this?



kanenas
Nov 20, 2010, 08:43 AM
display dialog (http://docs.info.apple.com/article.html?path=AppleScript/2.1/en/as305.html) "..." default answer "http://forums.macrumors.com/" will display a dialog with a text input. choose from list (http://docs.info.apple.com/article.html?path=AppleScript/2.1/en/as181.html) {...} will present a dialog with a list box. Unfortunately, there isn't any way of getting multiple inputs in a single dialog without installing a third party scripting addition. The usual approach is to let the user enter all the necessary information in the single input, separated by some character (e.g. a comma or space) With URLs, a space character would work well.

on getInput(refresh, |url|)
set oldDelims to text item delimiters
set text item delimiters to " "
display dialog "Enter refresh time (if you wish to override it) and URL, separated by spaces"
default answer (refresh & |url| as string)
set input to text returned of result
if (count of input's text items) > 1 then
try
set refresh to (text item 1 of input as integer)
set |url| to (text items 2 through end of input) as string
on error
set |url| to input
end try
else
try
set refresh to input as integer
on error
set |url| to input
end try
end if
set text item delimiters to oldDelims
return {refresh:refresh, URL:|url|}
end getInput

(Note: use [code] tags to delineate and format code, as above. If you ever post PHP code, use [php]. Other forums support other code tags, such as [html], but apparently not this one.)

Also read Apple's documentation on display dialog (http://developer.apple.com/library/mac/#documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html%23//apple_ref/doc/uid/TP40000983-CH216-DontLinkElementID_713) and choose from list (http://developer.apple.com/library/mac/#documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html).

jen234
Dec 1, 2010, 08:06 AM
If you have multiple sites and you need to do a random refresh (which is more natural), you may want to create a text file and read the input from that file rather than setting it up directly in your script.

That way, in the future, you simply modify your txt file if new URLs are to be added etc.