How to copy URL with page title from Safari to Pages
Solution 1:
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.