Recently I am trying to set up an AppleScript to do some simple task. But I can't really find some resources that can provide me with a systematic introduction or tutorial for me to get started with Apple Script. So I'd like to hear some advices on how should I get started. Or in other words which are the recommended materials or documentations that I should follow?

I tried to go through the Apple's official documentation, but that seems too heavy to be a quick start guide.

Something like this as we have in python would be the best.

And I just attach the actual problem that I am trying to solve below.

a simple logic:

if safari is not the front most application:
    bring safari to frontmost
    if certain webpage is opened in safari:
        bring that tab of the webpage to the frontmost
        open that webpage
    end if
    hide safari
end if

Quite simple and naive isn't it.

  • You can start by writing the word AppleScript properly. :) Seriously though, I'd start by, as they say RT*M, which in this case is from the Help menu in Script Editor: Show AppleScript Language Guide – user3439894 Feb 23 '18 at 15:45
  • I have to agree with you that AppleScript is completely unnatural - I don't think pragmatically or programmatically in their syntax. Personally, I have completely avoided AppleScript in favor of Bash scripting or PHP for all but the most necessary cases. Where I absolutely must use AppleScript, I have the paperback manual AppleScript 1-2-3. It's absolutely invaluable. – Allan Feb 23 '18 at 16:00
  • By the way, just because you can write simple pseudocode does't necessarily mean that when translated into the target language it's going to be simple! :) – user3439894 Feb 23 '18 at 16:01
  • Can you include the real code in your question so people can review and give feedback? Also, in terms of getting started: what have you tried so far? – nohillside Feb 23 '18 at 16:34
  • @patrix The OP is not asking how to fix code or even translate the pseudocode, so the real code, if there is any, isn't really relevant to the question asked, which was "So would anyone be able to tell me how should I properly start with AppleScript as a beginner?" which essentially calls for an opinion and why I voted to close this question. – user3439894 Feb 23 '18 at 17:02

Hey after some exploration, I figured out the code.

And I'd like to share it with you just in case someone else might have the same need.

This code basically toggles a pre-set webpage in safari. The detailed logic can be found in the pseudo code in the question description. And I believe it can be really useful if your are involving with web-wrapped applications a lot.

set appName to "Safari"
set pageName to "<+Your Page Name+>"
set pageUrl to "<+Your page URL+>"

tell application "System Events"
    if not (exists process appName) then
        tell application appName to activate
    else if frontmost of process appName then
        # only hide application if the current tab is what we want
        # my search_tabs(pageName, pageUrl)
        set visible of process appName to (my search_tabs(pageName, pageUrl))
        # still unable to give focus to desired window
        set frontmost of process appName to true
        my search_tabs(pageName, pageUrl)
    end if
end tell

on search_tabs(pageName, pageUrl)
    set tab_found to false
    set be_visible to false
    tell application "Safari"
        tell window 1
            if name of current tab starts with pageName then
                # if the current tab is already what we want then
                # return false
                return false
            end if
        end tell
        repeat with w in windows
            if name of w is not "" then --in case of zombie windows
                repeat with t in tabs of w
                    if name of t starts with pageName then
                        set tab_found to true
                        tell w to set current tab to t
                        if index of w is not 1 then
                            tell w to set index to 1
                        end if
                    end if
                end repeat
            end if
        end repeat
        # if the webpage doesn't exist
        if tab_found is false then
            tell window 1
                set current tab to (make new tab with properties {URL:pageUrl})
            end tell
        end if
        return true
    end tell
end search_tabs
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged .