Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
RTF saving and use
#1
Hi

i'd like to store RTF snippets (like RTF or HTML signature with image) in a file, then insert it
via clipboard using a macro and a hotkey but i don't know how to begin. The snippet would created in a RTF
aware application and pasted from clipboard to the data file storage. or any other mean
if better.

something like this

[img]     [/img]


Any ideas?

Thanks
#2
Function RtfSnippets
Code:
Copy      Help
;/
function~ action $databaseFile $snippetName ;;action: 0 copy/save, 1 paste, 2 delete, 3 get list

;Manages RTF snippets.
;Error if fails.
;With action 0 and 1, fails if the active window does not support RTF clipboard format.

;action:
;;;0 - gets selected text as RTF (copies through clipboard) and saves in database.
;;;;;Error if there is no text selected.
;;;1 - gets saved snippet and pastes as RTF.
;;;;;Error if the snippet (snippetName) does not exist, or if fails to paste.
;;;2 - deletes snippet. Not error if does not exist.
;;;3 - returns list of snippet names. For example, can be used to fill a combo box in a dialog. snippetName not used and can be "".
;databaseFile - SQLite database file.
;snippetName - snippet name in the file. Can be any string.

;Stores snippets in the database file, in table 'rtf', which has 2 columns: 'name' and 'data'.
;Creates the file and the table, if don't exist.
;If the snippet (snippetName) already exists in the table, updates.

;EXAMPLES
;RtfSnippets 0 "$my qm$\snippets.db3" "test1" ;;copy selected text and save as RTF snippet with name 'test1'
;err out _error.description

;RtfSnippets 1 "$my qm$\snippets.db3" "test1" ;;paste snippet 'test1'
;err


str s sn(snippetName)
sn.SqlEscape
Sqlite x.Open(databaseFile action&1)
ARRAY(str) a
int i

sel action
,case 0
,s.getsel(0 "Rich Text Format")
,if(!s.len) end "failed to copy text in RTF format"
,x.Exec("CREATE TABLE IF NOT EXISTS rtf (name TEXT PRIMARY KEY ON CONFLICT REPLACE, data TEXT)")
,x.Exec(F"INSERT INTO rtf VALUES ('{sn}', '{s.SqlEscape}')")
,
,case 1
,x.Exec(F"SELECT data FROM rtf WHERE name='{sn}' COLLATE NOCASE" a)
,if(!a.len) end F"snippet '{snippetName}' not found"
,a[0 0].setsel("Rich Text Format")
,
,case 2
,x.Exec(F"DELETE FROM rtf WHERE name='{sn}' COLLATE NOCASE" a)
,
,case 3
,x.Exec(F"SELECT name FROM rtf ORDER BY 1 COLLATE NOCASE ASC" a)
,for(i 0 a.len) s.addline(a[0 i])
,ret s
,
,case else
,end ES_BADARG

err+ end _error

tested with WordPad and Word 2003 on Windows XP and 7.
#3
Sorry for the late answer, but was busy...

first thanks for code, it works great but i'd like two improvements (if possible)

1. say i have 3 mail signatures sig1, sig2, sig3
i can make them in a db file via the previous code
i would like to type the word 'sig', and QM would then popup either a menu (or better tooltip)
contining the 3 signatures, and icould select the one i want with a number

2. can I make that dynamic, so any key sequence could be used, without
any keyboard trigger?

Thanks.
#4
This is not exactly what you need.
Requires some trigger.

Code:
Copy      Help
str dbFile="$my qm$\snippets.db3"
str s=RtfSnippets(3 dbFile "") ;;get list of snippet names from database
MenuPopup m.AddItems(s 1) ;;create menu
int i=m.Show(0 0 1) ;;show menu and wait
if(i=0) ret
m.GetItemText(i s) ;;get selected snippet name
RtfSnippets 1 dbFile s ;;get snippet from database, and paste

To enable keyboard for selecting menu items, use & character in snippet names. For example, if snippet name is "test&2", in menu it will be "test2", and you can press 2 to select it.
#5
Pretty close indeed, just few varations around that theme.

Does QM allows to do the same dynamic filling of a tooltip instead of a menu (esthetic will)
I don't have any database knowledge, but would it be possible to add some data fields in a record,
to make the dynamicmenu more flexible?

Fields of record: Category - Name - Data

Then:

Hotkey => Menu with category => wait for input => items in category => select => paste
|
Menu ----> Category I
----> Category II
----> Category III -----selection --- launch New menu ----> Category III
|____> item1
|____> item2 ----if i press numpad 2 => paste something
|____> etc

does it make sense?
#6
QM does not have tooltips that can be filled with a list of selectable items.

To add categories, include category names in snippet names, like "cat1.sig1". Use this macro.
Also please update your function RtfSnippets. The new version supports case insensitive names and returns sorted list. This macro uses these new features.

Macro rtf snippet menu3
Trigger <some keyboard trigger>     Help - how to add the trigger to the macro
Code:
Copy      Help
str dbFile="$my qm$\snippets.db3"
str s=RtfSnippets(3 dbFile "") ;;get list of snippet names from database

s.replacerx("^((.+?)\..+[](\2\..+[])*)" ">$2[]$1<[]" 1|8) ;;add submenus

MenuPopup m
m.AddItems(s 1) ;;create menu
m.AddItems("-[]Add snippet...[]Delete snippet...[]-[]Cancel" 10000)
int i=m.Show(0 0 1) ;;show menu and wait

sel i
,case [0,10004] ret ;;cancel
,
,case 10001 ;;add
,if(!inp(s "Snippet name. Can be category.name.[]RTF data will be copied from the active window." "QM - add or replace RTF snippet") or !s.len) ret
,RtfSnippets 0 dbFile s
,
,case 10002 ;;delete
,m.DeleteItems("10000-100004")
,i=m.Show; if(i=0) ret
,m.GetItemText(i s)
,RtfSnippets 2 dbFile s
,
,case else ;;paste
,m.GetItemText(i s) ;;get selected snippet name
,RtfSnippets 1 dbFile s ;;get snippet from database, and paste

err+ mes _error.description
#7
Ok i can live without the tooltip candy.

It's near what i wanted, i miss now:

- access menu item by keystroke (1 2 3 etc)
- it then opens a submenu containing the items to use, the main menu (MenuPopup m disappear then)
or opens a submenu with items, without disappearing. submenus items should be accessible to keys too.
- would a listbox replace the tooltip?

i'm nearly to destination i guess, few tweaks needed


Forum Jump:


Users browsing this thread: 1 Guest(s)