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?
Best Answer
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.
With a little practice, the following steps will become second nature:
Example AppleScript code:
Note: The example AppleScript code is just that and sans,
if not running of application "Safari" then return
andif 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.