Skip to content

Instantly share code, notes, and snippets.

@orschiro
Forked from hluk/CopyQ - Save As.ini
Last active September 25, 2015 06:38
Show Gist options
  • Save orschiro/2483c140e1bccbe0c238 to your computer and use it in GitHub Desktop.
Save orschiro/2483c140e1bccbe0c238 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().tempPath())
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...
GlobalShortcut=Ctrl+Shift+S
Shortcut=Ctrl+S
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment