Jump to content
techlectics

Rich Text in Snippets

Recommended Posts

At this time, snippets are plain text, and clipboard history can store plain text, images and file paths. :)

 

Cheers,

Vero

Share this post


Link to post

With the Snippet Trigger it is possible to do almost anything... It's not as easy as having a native way of inputting rich text into an Alfred native Snippets, but if you don't mind creating a workflow and a RTF file as your snippet content, then you can simply use a Run Script set to AppleScript that read the content of the file and put it into the clipboard and then paste it to the front application

 

Here is a workflow as example to show you how to do it (basically, just change the path to the file and set your own rtf file path to use your content): https://nofile.io/f/gK6eCRmeHCU/Example+-+Rich+Text+Snippet.alfredworkflow

 

Or, here is the main part of the Workflow to put the content of the RTF file into the clipboard, after that, just connect to a Dispatch Key Combo set to CMD+V to output to the front application

set theFilePath to "path/to/RTF/file.rtf"
set the clipboard to (read theFilePath as «class RTF »)

 

Edited by GuiB

Share this post


Link to post

@msw70, about HTML , just change the class to HTML ( read theFilePath as «class HTML» ):

 

set theFilePath to "path/to/HTML/file.html"
set the clipboard to (read theFilePath as «class HTML»)

 

Share this post


Link to post

@GuiBIt initially worked like a charm, but now the only thing is pastes is the file path and when I enter new items to the clipboard it seems that the whole script does not run, as the trigger for the rich text snippet pastes the last item of the clipboard. So the file contents seems not te be placed on the clipboard. Please advice?

 

Thanks in advance!

Reinier de Groot

Share this post


Link to post

@GuiB This is the debug error I get

 

Starting debug for 'Example: Rich Text Snippet'

[2018-02-12 16:28:08][ERROR: action.script] /Users/rebdeg/Library/Caches/com.runningwithcrayons.Alfred-3/Workflow Scripts/E38E264C-921F-4D1C-8399-8DA594CEF3E7:94:126: execution error: Er heeft zich een fout van het type -1409 voorgedaan. (-1409)

Share this post


Link to post

@rebdeg, try adding a delay before pasting. It was working great without on my side, but yes, if your snippets takes a little more time to copy, then a delay will help. Try this new workflow (change de delay time to a value that work better on your side): https://nofile.io/f/NvOjpgwVFOC/Example+-+Rich+Text+Snippet.alfredworkflow

 

Edited by GuiB

Share this post


Link to post

@GuiB I stil get this debug error:

[2018-02-12 16:39:58][ERROR: action.script] /Users/rebdeg/Library/Caches/com.runningwithcrayons.Alfred-3/Workflow Scripts/4A2AF514-1F9F-4A64-8C8D-2C2B6B48547F:96:128: execution error: "/Users/rebdeg/Documents/Werkmap%20NSI/EmailHand.rtfd" kan niet in type file worden omgezet. (-1700)

 

Any thoughts?

 

Regards,

Reinier

Share this post


Link to post

@rebdeg, You're trying to expand a RTFD file (Rich Text Format Directory) instead of a simpler RTF file. RTFD are document, so contains the RTF file inside + associated images... I need to go at the moment, but will have a look later when I come back.

Share this post


Link to post

@rebdeg, ok, I went back home and had a look at this... it's more complex and I'll give another example if I find a simpler way to do it, but I got it working with some AppleScriptObjC. But in short, on your side you just have to change the file path on the first line and it should work.

 

Look here for a Workflow as example: https://nofile.io/f/RXhJKD2bpYC/Example+-+Rich+Text+Snippet.alfredworkflow

 

The main part is this (the AppleScript inside the Run Script is now this):

 

set theFilePath to "path/to/your/RTFD/file.rtfd"

------------------------------------
use framework "Foundation"
use scripting additions

set pboard to its NSPasteboard's generalPasteboard()
pboard's clearContents()
set theURL to its (|NSURL|'s fileURLWithPath:theFilePath)
set fileWrapper to its (NSFileWrapper's alloc()'s initWithURL:theURL options:0 |error|:(missing value))
set attrString to its (NSMutableAttributedString's alloc()'s initWithRTFDFileWrapper:fileWrapper documentAttributes:(missing value))
set theData to attrString's RTFDFromRange:{location:0, |length|:attrString's |length|()} documentAttributes:{DocumentType:"NSRTFD"}
pboard's setData:theData forType:(its NSPasteboardTypeRTFD)

 

Edited by GuiB

Share this post


Link to post

@GuiB I was trying to go to your links, however, was not able to open the workflow example in my Alfred 3. I'm having the same issue. 

  1. I'm looking to have an RTF signature saved as a snippet. Then, would need to paste it whenever I need. 

I'd love it if you're able to guide me through a step by step process to achieve it through your method. 

 

Thanks a million

Share this post


Link to post

@eternali, you mean the download isn't working on your side ? Or that you are not able to install it in Alfred ? Or that you are not able to configure it to make it work with your own RTF signature ?

 

Here is a new workflow that use User Environment Variable, so it may be easier for you to figure out where to put your file path.

https://nofile.io/f/UPw0gDKn82H/Example+-+Rich+Text+Snippet.alfredworkflow

 

Here are some cues to use it:

 

To install:

To Use with a RTF file:
  • Create a new RTF file using Text Edit
  • Save the file somewhere on your computer
  • Open Alfred Workflows and look for the Example - Rich Text Snippet
  • Open the workflow configuration menu (button: "[x]" )
  • Set the "filePathToRTF" variable to the path of your created RTF document
  • Change the snippet to one that you want (double click the first node of the workflow)
  • Then, you should be able to open another application, do you snippet and automatically expand to your RTF content
For RTFD:
  • Do the same thing as for a RTF file, but save as RTFD and set the "filePathToRTFD" variable to your RTFD document path

Share this post


Link to post
27 minutes ago, GuiB said:

you mean the download isn't working on your side ?

 

Have you considered putting your workflows on GitHub or similar (with source code)?

 

Nofile, Dropbox etc. are better suited for throwaway workflows, IMO, which isn’t a description that applies to your stuff. A “proper” host makes it easier for folks to find the latest version, too.

Share this post


Link to post

@GuiBThank you very much this one worked like a charm. 

 

  1. Actually, two of the files were not downloadable. 
  2. When I managed to download one of them, it did not install with Alfred. 

I guess, what @deanishe has suggested would be great. Get yourself a good GitHub repository! :D 

 

Thanks for helping out, 

PS: Guillaume, I would've loved to 'buy you a coffee' however, I live in a country where PayPal is 'banned' so I'm not able to transfer a gift to you. 

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...