4

In earlier versions of Safari and Pages drag&drop of an URL from Safari to Pages resulted in a link with the page title as the text, and the URL as a link "behind" it. It still works that way in Mail in Mojave.

But with Safari and Pages all you get with drag&drop is a link (without the page title), which you then need to edit to replace the displayed text with i.e. the page title.

Is there a way (maybe via AppleScript) to insert title & URL of a Safari tab into a Pages document without having to manually edit it afterwards?

1
  • Welcome to AskDifferent! I did a rather heavy-handed edit to focus your question on the topic at hand. In case I missed something please just edit it back in.
    – nohillside
    Commented Jun 29, 2019 at 12:14

3 Answers 3

2

The ideal solution, with the current tab of the front window of Safari set to the desired address, one would then in Pages press a keyboard shortcut to, at the cursors present location, create a website link matching the documents style (font size, etc,) having the Title and URL of the Safari target pasted into the Pages document.

Off the top of my head, if that's even doable, it would probably require using AppleScriptObjC, which unfortunately is not something I know how to do, rather than using plain vanilla basic AppleScript.

That said, let me offer an example that works for me in macOS High Sierra and Pages 7.1.

Using an Automator service1 with a Run AppleScript action, set for Pages, and assigning it a keyboard shortcut in System Preference > Keyboard > Shortcuts > Services, the example AppleScript code, shown further below, creates a website link on its own line directly after the line the cursor is on when the keyboard shortcut is pressed.

With this particular code, it needs to be done in this manner; however, one can then cut and paste it where desired in the document and remove the extra line added to the document. It's not the ideal solution, but it does get the job done with the least amount of hassle while using this particular method.

  • In Automator, create a new Service1, setting: Service receives [no input] in [Pages]
    • 1 In macOS Mojave, an Automator service is now called a Quick Action.
  • Add a Run AppleScript action, replacing the default code with the example AppleScript code, shown further below.
  • Save the Automator service as, e.g.,: Add Safari Weblink to Pages
  • In System Preference > Keyboard > Shortcuts > Services > e.g. Add Safari Weblink to Pages, assign it a keyboard shortcut, e.g.,: ^⌘L (Control-Command-L)
    • Note: I wanted the least number of keys to press and something close to the default ⌘K, and ^⌘L was it. It also does not step on any other default keys in Pages.
  • In Pages, with a document opened, press ^⌘L to create the website link of the current tab of the frontmost Safari window.

With a little practice, the following steps will become second nature:

  • Select the target webpage in Safari, letting it be the frontmost Safari window.
  • In Pages, with a document opened, press:
    • ^⌘L, and wait until Add Link pop-up dialog disappears.
    • ⌘X
    • delete (Removes the added line.)
    • Set the cursor to where you want the website link inserted.
    • ⌘C
  • This should only take but a few seconds total.

Example AppleScript code:

if not running of application "Safari" then return

tell application "Safari" to set |Title & URL| to {name, URL} of current tab of front window
delay 0.1

set the clipboard to first item of |Title & URL| -- Title of front Safari document.
delay 0.1

tell application "Pages"
    activate
    delay 0.1
    if name of front window is in {"Choose a Template", ""} then return
end tell

tell application "System Events"
    key code 124 using {command down} -- Right Arrow Key - Send cursor to end of line.
    delay 0.1
    key code 36 -- Enter Key - Send cursor to beginning of new line.
    delay 0.1
    keystroke "v" using {option down, shift down, command down} -- Paste and Match Style. (Title of front Safari document.)
    delay 0.1
    key code 123 using {shift down, command down} -- Left Arrow Key - Highlight pasted Title.
    delay 0.1
    tell current application to set the clipboard to second item of |Title & URL| -- URL of front Safari document.
    delay 0.1
    keystroke "k" using {command down} -- Pages > Format > Add Link > Webpage -- (When some text is highlighted.)
    delay 0.1
    keystroke "v" using {command down} -- Paste (URL of front Safari document.)
    delay 0.1
    key code 36 -- Enter Key - Needed to set pasted URL, otherwise it stays as the default URL.
    delay 0.1
    key code 53 -- Escape Key - Dismiss the Add Link pop up dialog box.
end tell

set the clipboard to ""
  • Note: The example AppleScript code makes use of the Clipboard in order to use Paste and Match Style in the Pages document, and then clears the Clipboard. Keep this in mind so as not to loose something that's there and nowhere else if applicable. Additionally, if the length of Title exceeds the length of the line it's pasted to, only the last line of the pasted Title will be highlighted when the website link is created. This is one of the drawbacks of using just plain vanilla basic AppleScript verses AppleScriptObjC.

Note: The example AppleScript code is just that and sans, if not running of application "Safari" then return and if name of front window is in {"Choose a Template", ""} then return, it does not contain any additional 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.

You might also find that the value of the delay command, in some instances, may need to be adjusted for use on your system, adjust as/if necessary.


Add Safari Weblink to Pages

1

A general solution is to combine your AppleScript (Script Editor)(/System/Applications/Utilities/Script Editor.app) with an app launcher like Alfred.

Script

tell application "Safari" to tell document 1
    set the clipboard to "[#]: " & URL & " \"" & name & "\""
end tell

In this case, the result string is formatted as the second part of a Markdown reference-style link. E.g.:

[link text][1]

[1]: url "title"

Hotkey Trigger

  1. In your app launcher, create a hotkey workflow
  2. In Alfred, click Settings > Workflows, then click + > Blank workflow
  3. Add a Workflow Name; other values are optional
  4. Click + > Triggers > Hotekey
    1. In the Hotkey Settings tab, assign a Hotkey value. E.g. ⌥C
    2. Leave Action as Pass through to workflow or your launcher's equivalent
    3. Leave Argument set to None.
    4. Go to the Related Apps tab
    5. Verify have focus is the value assigned to Hotkey is...active when..apps
    6. Click Save
  5. Click + Actions > Run Script
  6. Set Language to /usr/bin/osascript (AS); leave other options set to their default values
  7. Paste your version of the above script into the Script box
  8. Click Save
  9. Drag the Hotkey box's node and connect it to the Run Script box

That's it! Now clicking ⌥C in Safari lets you paste URL and title values in any app in any format with the standard Paste keyboard shortcut — ⌘P.

As above, you should be able to run your script as a service. You can assign a shortcut key to run the service by going to System Preferences > Keyboard > Services.

PS: You can also download my Alfred Workflow and scripts (plain text and .scpt) from GitHub.

1
  • This workflow finally give me the chance to stop using Chrome because I'm constantly sharing url with title.
    – Samuel
    Commented Jul 26, 2023 at 17:07
-1

I had a similar problem, I wanted to copy the page title, so to solve it I made an Automator Quick Action which appears in Safari's Safari->Services menu. It gets the page title and puts it in the clipboard. It probably could be improved to also get the link. Note the "no input" choice.

enter image description here

enter image description here

tell application "Safari"
    set mytitle to name of current tab in window 1
end tell

return mytitle

I got the Apple Script from this answer.

You must log in to answer this question.

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