1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Applescript NOOB

Discussion in 'Mac Programming' started by Revolume, Nov 19, 2010.

  1. macrumors newbie

    #1
    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?
     
  2. kanenas, Nov 20, 2010
    Last edited: Nov 20, 2010

    macrumors newbie

    #2
    "display dialog"

    display dialog "..." default answer "http://forums.macrumors.com/" will display a dialog with a text input. choose from list {...} 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.

    Code:
    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 [URL="http://developer.apple.com/library/mac/#documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html%23//apple_ref/doc/uid/TP40000983-CH216-DontLinkElementID_713"][FONT="Courier New"]display dialog[/FONT][/URL] and [URL="http://developer.apple.com/library/mac/#documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html"][FONT="Courier New"]choose from list[/FONT][/URL].
     
  3. macrumors newbie

    #3
    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.
     

Share This Page