Skip to content

Instantly share code, notes, and snippets.

@hluk
Last active March 18, 2022 14:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hluk/64dae8b25cd21e6141dd to your computer and use it in GitHub Desktop.
Save hluk/64dae8b25cd21e6141dd to your computer and use it in GitHub Desktop.
CopyQ - Save Item/Clipboard To a File
[Command]
Command="
copyq:
var suffices = {
'image/svg': 'svg',
'image/png': 'png',
'image/jpeg': 'jpg',
'image/jpg': 'jpg',
'image/bmp': 'bmp',
'text/html': 'html',
'text/plain' : 'txt',
}
function hasSuffix(fileName)
{
return /\\.[0-9a-zA-z]+$/.test(fileName);
}
function addSuffix(fileName, format)
{
var suffix = suffices[format]
return suffix ? fileName + \".\" + suffix : fileName
}
function filterFormats(format)
{
return /^[a-z]/.test(format) && !/^application\\/x/.test(format)
}
function itemFormats(row)
{
return str(read('?', row))
.split('\\n')
.filter(filterFormats)
}
function formatPriority(format)
{
var k = Object.keys(suffices);
var i = k.indexOf(format);
return i === -1 ? k.length : i
}
function reorderFormats(formats)
{
formats.sort(function(lhs, rhs){
var i = formatPriority(lhs);
var j = formatPriority(rhs);
return i === j ? lhs.localeCompare(rhs) : i - j;
})
}
if (selectedtab()) tab(selectedtab())
var row = selectedtab() ? currentitem() : -1
var formats = itemFormats(row)
reorderFormats(formats)
currentpath(Dir().homePath())
var defaultFileName = 'untitled'
var keyFormat = 'Format'
var keyFileName = 'File'
var defaultFormat = formats[0]
var result = dialog(
'.title', 'Save Item As...',
'.width', 250,
keyFormat, [defaultFormat].concat(formats),
keyFileName, File(defaultFileName)
) || abort()
var fileName = result[keyFileName]
var format = result[keyFormat]
if (!format || !fileName)
abort()
if (!hasSuffix(fileName))
fileName = addSuffix(fileName, format)
var f = File(fileName)
if (!f.open()) {
popup('Failed to open \"' + f.fileName() + '\"', f.errorString())
abort()
}
f.write(selectedtab() ? getitem(currentitem())[format] : clipboard(format))
popup(\"Item Saved\", 'Item saved as \"' + f.fileName() + '\".')"
Icon=\xf0c7
InMenu=true
Name=Save As...
Shortcut=Ctrl+S
@hluk
Copy link
Author

hluk commented Mar 24, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment