1

Will this applescript work?

Tell Application "Safari" to open location
"http://www.site.com/"
end tell

Tell Process "Safari"
   Keystroke "l" using command down

Tell application "System Events" to key code
124 using command down

Tell Process "Safari"
   Keystroke "v" using command down
   Keystroke return
end tell
  • Have you run it? Did you get an error? Did it do anything? – Solar Mike May 29 at 18:56
  • I don't have a mac actually. Ive wrote many powershell scripts and was just trying to help a friend out by writing a applescript. It looks good, but was looking for validation from someone else that it should work as intended. – Ryan May 29 at 19:04
  • @Ryan there are some syntax errors. Please see other threads to see how to fix it. Then ping me, I can test it. This is useful for me too. apple.stackexchange.com/questions/tagged/applescript apple.stackexchange.com/questions/363521/… – anki May 29 at 19:19
  • @Ryan this works for the most part: pastebin.com/nmKd8pBm – anki May 29 at 19:35
  • That looks good. What do you mean for the most part? Are there any errors or does it work hit or miss? – Ryan May 29 at 19:37
2

The following example AppleScript code will do as you asked:

set the clipboard to "questions/392514/i-want-to-write-a-script-to-open-a-fixed-url-whats-in-the-clipboard-ex-open"

set myURL to "https://apple.stackexchange.com/" & (the clipboard)

tell application "Safari" to ¬
    tell its first window to ¬
        set its current tab to ¬
            (make new tab with properties {URL:myURL})

If you run the example AppleScript code, as is, it will open to your question.

The first line of code is just there for testing purposes as well as the "https://apple.stackexchange.com/" part of myURL. Change it to what you want and comment out or delete the first line to use it normally.

To incorporate that into something more robust to account for the current state of Safari, the following example AppleScript code handles the typical different scenarios:

set the clipboard to "questions/392514/i-want-to-write-a-script-to-open-a-fixed-url-whats-in-the-clipboard-ex-open"

set myURL to "https://apple.stackexchange.com/" & (the clipboard)

tell application "Safari"
    activate
    if (count documents) is equal to 0 then
        make new document
        repeat until exists its first window
            delay 0.01
        end repeat
        set URL of its current tab of its first window to myURL
    else
        set firstTabURL to URL of its first tab of its first window
        if {"favorites://", "topsites://", missing value} contains firstTabURL then
            set URL of its current tab of its first window to myURL
        else
            tell its first window to ¬
                set its current tab to ¬
                    (make new tab with properties {URL:myURL})
        end if
    end if
end tell 

Again, the first line of code is just there for testing purposes as well as the "https://apple.stackexchange.com/" part of myURL. Change it to what you want and comment out or delete the first line to use it normally.

Note: The example AppleScript code was tested on macOS High Sierra.


Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.

| improve this answer | |
0

This hack also works.

Bind this to a keyboard shortcut using one of these answers:

tell application "Safari"
    tell window 1 to make new tab
    open location "site.com"
end tell

delay 0.5

tell application "System Events"
    delay 0.5
    keystroke "l" using command down
    key code 124 -- right key, since URL is selected already
    keystroke "v" using command down
    delay 1
    key code 36 -- enter. But beware, it can "enter" any popup that appears too.
end tell
| improve this answer | |
  • RE: "This hack also works." -- You are right, it's a hack! 1. Safari does not understand the open location command because it is not a part of its AppleScript dictionary, it's a part of Standard Additions and doesn't belong in a tell application "Safari" block. If another browser was the default it would open in the other browser, not Safari. 2. This construct keystroke (key code 124) is not at all necessary, simply key code 124 is all that's necessary, and the same goes for the other key code command in your code. Continued in next comment... – user3439894 May 30 at 12:14
  • 3. There is absolutely no reason to be using UI Scripting in this use case because Safari has the vocabulary along with the clipboard command from Standard Additions to accomplish this simply in a very clean manner as shown in my answer. There is no need to resort to using UI Scripting, and its should only be used when there is no other, or more proper, way to accomplish the task! – user3439894 May 30 at 12:14
  • I only modified what I got in the question. // thanks for not downvoting it (: – anki May 30 at 12:36
  • Re 1 , for some reason, focus was shifting back to original when using tell its first window to set its current tab to (make new tab with properties {URL:myURL}) when I tried it yesterday. So I broke it into two parts. – anki May 30 at 12:41
-1

I ended up going with this.

set this_text to the clipboard   
set theURL to "[url]http://www.site.com/[/url]" & this_text

tell application "Safari"    
   activate    
   if (count documents) is 0 then make new document    
   tell window 1
       set tabTest to URL of tab 1    
       try
           tabTest -- throws an error if the window has no tabs   
           set current tab to (make new tab with properties{URL:theURL})    
       on error    
           set URL of tab 1 to theURL    
       end try    
   end tell

end tell

| improve this answer | |
  • Can you explain in more details what the code does? – nohillside May 31 at 12:46
  • I have to agree with nohillside, because I fail to see the logic in your code, in particular why the set tabTest to URL of tab 1 and the try statement that follows. If Safari has any browser windows open then there is always a tab 1 and the on error will never trigger. Additionally "[url]http://www.site.com/[/url]" is malformed and will never open because of [url] and [/url]. Please see my updated answer that handles the typical different scenarios of the state of Safari. – user3439894 May 31 at 14:04

You must log in to answer this question.

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