Skip to content

Instantly share code, notes, and snippets.

@hluk
Last active March 24, 2017 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hluk/be4481e6c72ab607c074 to your computer and use it in GitHub Desktop.
Save hluk/be4481e6c72ab607c074 to your computer and use it in GitHub Desktop.
CopyQ - Save Texts on Disk
[Command]
Name=Save Texts from Tab
Command="
copyq:
function encodeFileName(name)
{
return name.replace(/\\s/g, '_').replace(/\\W/g, '')
}
function addFileNamePart(fileName, item, mime)
{
var text = str(item[mime] || \"\").substring(0, 20)
if (text)
return fileName + (fileName ? \"_\" : \"\") + encodeFileName(text)
return fileName
}
var savePathKey = 'SaveTextsPath'
var savePath =
settings(savePathKey) || (Dir().homePath() + '/copyq_items/')
savePath = dialog(
'.title', 'Save Texts to Folder',
'.width', 320,
'Folder to Save Texts', File(savePath)
)
if (!savePath)
abort()
settings(savePathKey, savePath)
var mime = 'text/plain'
var tabName = selectedtab()
var date = new Date()
var path =
savePath + '/' + encodeFileName(tabName) + '_' +
date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()
tab(tabName)
Dir(path).mkpath('.')
var count = 0;
for (var i = 0; i < size(); ++i) {
var item = getitem(i)
var text = item[mime]
if (!text)
continue
var fileName = \"\"
// window title
fileName = addFileNamePart(fileName, item, 'application/x-copyq-owner-window-title')
// copied time
fileName = addFileNamePart(fileName, item, 'application/x-copyq-time')
// row number
fileName += (fileName ? \"_\" : \"\") + i
fileName = path + '/' + fileName + '.txt'
var f = File(fileName)
if (!f.open()) {
popup('Failed to open \"' + f.fileName() + '\"', f.errorString())
abort()
}
f.write(text)
++count
}
popup(
'Tab saved (' + count + ' items)',
'Items saved in \"' + path + '\"'
)"
InMenu=true
Icon=\xf0c7
@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