• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Applescript & RTF & TextEdit...
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Applescript & RTF & TextEdit...


  • Subject: Re: Applescript & RTF & TextEdit...
  • From: Shane Stanley <email@hidden>
  • Date: Tue, 18 Apr 2017 21:50:55 +1000

On 18 Apr 2017, at 6:15 pm, Jean-Christophe Helary <email@hidden> wrote:

TE offers an "Add Link" function (Cmd+K), if you trigger it, you get a dialog that asks for an URI. When you paste a local file in the dialog, TE only pastes the path and not the "file://" part that would be necessary to have TE recognize the link as a link to a local file (if you omit the "file://" it says that it doesn't know which app to use to open the file).

So the idea is to complement that "Add Link" function with something that adds the "file://" part for local paths. I understand that *this* does not require fiddling with the RTF code (although that is possible to do it there too, after the link is entered).

The problem with that dialog is that it adds a link where the text matches the URL exactly, file://, s, and all. Better to skip it.

You say you're pasting the file, so I assume you've copied it in the Finder. Try this:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

-- get the clipboard
set theClip to current application's NSPasteboard's generalPasteboard()
-- get URLs off clipboard
set theURLs to (theClip's readObjectsForClasses:{current application's |NSURL|} options:(missing value))
if (count of theURLs) = 0 then
display dialog "No files found on the clipboard" buttons {"OK"} default button 1
error number -128
end if
set theURL to theURLs's firstObject()
set thePath to theURL's |path|()
-- make mutable attributed string
set attString to current application's NSMutableAttributedString's alloc()'s initWithString:thePath
-- style it as a link
attString's addAttribute:(current application's NSLinkAttributeName) value:(theURL's absoluteString()) range:{0, attString's |length|()}

-- get the clipboard
set theClip to current application's NSPasteboard's generalPasteboard()
-- clear clipboard and write new attributed string to it
theClip's clearContents()
theClip's writeObjects:{attString}

-- use GUI scripting to paste into document
tell application "TextEdit" to activate
tell application "System Events"
tell process "TextEdit"
keystroke "v" using command down
end tell
end tell

-- 
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>, <latenightsw.com>


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Applescript & RTF & TextEdit...
      • From: Jean-Christophe Helary <email@hidden>
References: 
 >Applescript & RTF & TextEdit... (From: Jean-Christophe Helary <email@hidden>)
 >Re: Applescript & RTF & TextEdit... (From: Shane Stanley <email@hidden>)
 >Re: Applescript & RTF & TextEdit... (From: Jean-Christophe Helary <email@hidden>)

  • Prev by Date: Re: Sorting characters of the text - script doesn't work as expected (Yvan KOENIG)
  • Next by Date: Re: Applescript & RTF & TextEdit...
  • Previous by thread: Re: Applescript & RTF & TextEdit...
  • Next by thread: Re: Applescript & RTF & TextEdit...
  • Index(es):
    • Date
    • Thread