Last active
November 7, 2024 19:53
-
-
Save hluk/af37cd2ca650c92ed0fa to your computer and use it in GitHub Desktop.
CopyQ - Dialog for Pasting Snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Command] | |
Command=" | |
copyq: | |
var snippetsTabName = 'Snippets' | |
function newVarRe(content) { | |
return new RegExp('\\\\${' + content + '}', 'g') | |
} | |
function getText(item) { | |
var textData = item['text/plain'] | |
return textData ? str(textData) : '' | |
} | |
function assignPlaceholder(snippet, placeholder, value) { | |
return snippet.replace(newVarRe(placeholder), value) | |
} | |
function fuzzyIndexOf(snippetNames, snippetName) { | |
var re = new RegExp(snippetName, 'i') | |
for (var i in snippetNames) { | |
if (snippetNames[i].match(re)) | |
return i; | |
} | |
return -1 | |
} | |
function loadSnippets(snippetNames, snippets) | |
{ | |
var tabs = tab() | |
for (var i in tabs) { | |
var tabName = tabs[i]; | |
if (tabName != snippetsTabName && tabName.indexOf(snippetsTabName + '/') != 0) | |
continue; | |
tab(tabName) | |
var prefix = tabName.substring(snippetsTabName.length + 1) | |
for (var j = 0; j < size(); ++j) { | |
var snippet = getitem(j) | |
var snippetName = str(snippet['application/x-copyq-item-notes']) || getText(snippet) | |
if (prefix.length != 0) | |
snippetName = prefix + ': ' + snippetName | |
snippetNames.push(snippetName) | |
snippets.push(snippet) | |
} | |
} | |
} | |
function askForSnippetName(snippetNames) { | |
return str(dialog( | |
'.title', 'Snippets', | |
'Snippet', [snippetNames[0]].concat(snippetNames) | |
) || abort()) | |
} | |
function askForSnippet(snippetNames, snippets) { | |
var snippetName = askForSnippetName(snippetNames) | |
var i = snippetNames.indexOf(snippetName) | |
if (i == -1) { | |
i = fuzzyIndexOf(snippetNames, snippetName) | |
if (i == -1) { | |
popup( | |
'Snippet Not Found', | |
'No matching snippet found for \"' + snippetName + '\"!' | |
) | |
abort() | |
} | |
} | |
return snippets[i] | |
} | |
function getPlaceholders(snippet) { | |
var placeholders = [] | |
var m | |
var reVar = newVarRe('(.*?)') | |
while ((m = reVar.exec(snippet)) !== null) { | |
if (placeholders.indexOf(m[1]) === -1) | |
placeholders.push(m[1]) | |
} | |
return placeholders | |
} | |
function askToAssignPlaceholders(snippet) { | |
var placeholders = getPlaceholders(snippet) | |
if (placeholders.length > 0) { | |
var dialogVars = [ | |
'.title', 'Snippet Values for \"' + snippet + '\"'] | |
for (var i in placeholders) { | |
dialogVars.push(placeholders[i]) | |
dialogVars.push(\"\") | |
} | |
var values = dialog.apply(this, dialogVars) || abort() | |
if (placeholders.length > 1) { | |
for (var i in placeholders) | |
snippet = assignPlaceholder(snippet, placeholders[i], values[placeholders[i]]) | |
} else { | |
snippet = assignPlaceholder(snippet, placeholders[0], values) | |
} | |
} | |
return snippet | |
} | |
function pasteSnippet(mime, content) { | |
copy(mime, content) | |
copySelection(mime, content) | |
paste() | |
} | |
var snippetNames = [] | |
var snippets = [] | |
loadSnippets(snippetNames, snippets) | |
var snippet = askForSnippet(snippetNames, snippets) | |
var textSnippet = getText(snippet) | |
if (textSnippet) | |
pasteSnippet('text/plain', askToAssignPlaceholders(textSnippet)) | |
else | |
pasteSnippet('application/x-copyq-item', pack(snippet))" | |
GlobalShortcut=Meta+Alt+Q | |
Icon=\xf1fb | |
Name=Snippets |
I think I've just figured out:
- install the plugin
- create tab called 'Snippets'
- add items to it, the mnemonic is the item note
- invoke it with Meta+Alt+Q, choose snippet and it will paste it.
Moved to copyq-commands repository.
Is there code to auto hide the main window initiating the Global Shortcut for this?
(currently on MacOS)
I just got a private message from someone having issues so this is my response:
I can't remember what I did but:
- there must be a tab (Snippets),
- the tab needs to contain at least one item
- all the items should have notes attached (Shift+F2).
I have attached my slightly modified version with some error handling I added when getting this to work:
///////////////////////////////////////////////////////////////////////////////////////
// https://gist.github.com/hluk/af37cd2ca650c92ed0fa
// The following code came from here, Dennis Bareis
// added enough error handling code to work out the
// problems he had getting it going...
// https://www.youtube.com/watch?v=xVUzM7TjV_g&t=158s
///////////////////////////////////////////////////////////////////////////////////////
var snippetsTabName = 'Snippets'
function newVarRe(content) {
return new RegExp('\\${' + content + '}', 'g')
}
function getText(item) {
var textData = item['text/plain']
return textData ? str(textData) : ''
}
function assignPlaceholder(snippet, placeholder, value) {
return snippet.replace(newVarRe(placeholder), value)
}
function fuzzyIndexOf(snippetNames, snippetName) {
var re = new RegExp(snippetName, 'i')
for (var i in snippetNames) {
if (snippetNames[i].match(re))
return i;
}
return -1
}
function loadSnippets(snippetNames, snippets)
{
var tabs = tab()
for (var i in tabs) {
var tabName = tabs[i];
if (tabName != snippetsTabName && tabName.indexOf(snippetsTabName + '/') != 0)
continue;
tab(tabName)
var prefix = tabName.substring(snippetsTabName.length + 1)
for (var j = 0; j < size(); ++j) {
var snippet = getitem(j)
var snippetName = str(snippet['application/x-copyq-item-notes']) || getText(snippet)
if (snippetName == "undefined")
snippetName = "[NOTE MISSING ON SNIPPET]"
if (prefix.length != 0)
snippetName = prefix + ': ' + snippetName
snippetNames.push(snippetName)
snippets.push(snippet)
}
}
}
function askForSnippetName(snippetNames) {
return str(dialog(
'.title', 'Snippets',
'Snippet', [snippetNames[0]].concat(snippetNames)
) || abort())
}
function askForSnippet(snippetNames, snippets) {
var snippetName = askForSnippetName(snippetNames)
var i = snippetNames.indexOf(snippetName)
if (i == -1) {
i = fuzzyIndexOf(snippetNames, snippetName)
if (i == -1) {
popup(
'Snippet Not Found',
'No matching snippet found for "' + snippetName + '"!'
)
abort()
}
}
return snippets[i]
}
function getPlaceholders(snippet) {
var placeholders = []
var m
var reVar = newVarRe('(.*?)')
while ((m = reVar.exec(snippet)) !== null) {
if (placeholders.indexOf(m[1]) === -1)
placeholders.push(m[1])
}
return placeholders
}
function askToAssignPlaceholders(snippet) {
var placeholders = getPlaceholders(snippet)
if (placeholders.length > 0) {
var dialogVars = [
'.title', 'Snippet Values for "' + snippet + '"']
for (var i in placeholders) {
dialogVars.push(placeholders[i])
dialogVars.push("")
}
var values = dialog.apply(this, dialogVars) || abort()
if (placeholders.length > 1) {
for (var i in placeholders)
snippet = assignPlaceholder(snippet, placeholders[i], values[placeholders[i]])
} else {
snippet = assignPlaceholder(snippet, placeholders[0], values)
}
}
return snippet
}
function pasteSnippet(mime, content) {
copy(mime, content)
copySelection(mime, content)
paste()
}
var snippetNames = []
var snippets = []
loadSnippets(snippetNames, snippets)
//--- DB$: Exit if no Snippets exist ----------------------------------------
if (snippets.length == 0)
{
var Msg = 'You haven\'t defined any snippets in the "Snippets" tab yet!'
serverLog(Msg)
popup(Msg, "", 5000)
abort()
}
var snippet = askForSnippet(snippetNames, snippets)
var textSnippet = getText(snippet)
if (textSnippet)
pasteSnippet('text/plain', askToAssignPlaceholders(textSnippet))
else
pasteSnippet('application/x-copyq-item', pack(snippet))
Is it possible to have also have computed fields / that dynamically generate based on what was input for another field?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Again, looks good, but nothing to explain what it does and how to set it up. I guessed that I needed to create a "Snippets" tab and put something there but now the command just shows "Undefined". I will probably work it out but I agree with all the reviewers that this tool has a very steep learning curve.
CopyQ seems like an amazing product but is definately let down by its usability, I haven't even found a command repository yet but I'm still hopeful.