Skip to content

Instantly share code, notes, and snippets.

@davebrny
Last active October 25, 2023 13:06
Show Gist options
  • Save davebrny/6746782dc720820cd61a552f470fe9cf to your computer and use it in GitHub Desktop.
Save davebrny/6746782dc720820cd61a552f470fe9cf to your computer and use it in GitHub Desktop.
(autohotkey) - improvements for lightshot (screenshot tool)
/*
[folders]
folder_1=
folder_2=
folder_3=
[settings]
prefix = LS_
time_format = yyyyMMddhhmmss
fullscreen_hotkey =
upload_hotkey =
[script info]
version = 0.4.3
lightshot = 5.4.0.1
description = improvements for lightshot (app.prntscr.com)
author = davebrny
source = https://gist.github.com/davebrny/6746782dc720820cd61a552f470fe9cf
*/
#noEnv
#singleInstance, force
#installKeybdHook
sendMode, input
process, priority, , high
loop, HKLM, SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList, 1, 1 ; get user SID
{
if (a_loopRegName = "ProfileImagePath")
{
regRead, output
if inStr(output, a_userName)
{
stringReplace, user_sid, a_loopRegSubKey, SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\
break
}
}
}
lightshot_subkey := user_sid . "\Software\SkillBrains\Lightshot"
iniRead, prefix, % a_lineFile, settings, prefix
iniRead, time_format, % a_lineFile, settings, time_format
iniRead, custom_menu, % a_lineFile, settings, custom_menu
iniRead, fullscreen, % a_lineFile, settings, fullscreen_hotkey
iniRead, upload, % a_lineFile, settings, upload_hotkey
if (fullscreen) ; make hotkeys pass through to lightshot
hotkey, ~%fullscreen%, rename_with_date
if (upload)
hotkey, ~%upload%, rename_with_date
gui +lastFound
dllCall("RegisterShellHookWindow", UInt, winExist()) ; code from SKAN
onMessage(dllCall("RegisterWindowMessage", Str, "SHELLHOOK"), "shellMessage")
return ; end of auto-execute ------------------------------------------------
#if winActive("ahk_exe Lightshot.exe", , "Save As")
l::
; rButton::
goSub, custom_menu
return
#if winActive("ahk_exe Lightshot.exe", , "Save As")
enter::
$^s::
send ^{s}
goSub, rename_with_date
return
#if
rename_with_date:
sleep 700 ; wait for file to appear to
file_ext := file_ext()
loop, files, % save_folder() "\Screenshot_*" . file_ext
{
formatTime, formatted_time, % a_loopFileTimeModified, % time_format
fileMove, % a_loopFileFullPath, % save_folder "\" prefix . formatted_time . file_ext
}
return
file_ext() {
global lightshot_subkey
regRead, dword, HKU, % lightshot_subkey, Format
file_format := (dword = "1") ? "png" : (dword = "2" ? "jpg" : "bmp")
return "." file_format
}
save_folder() {
global
if (save_folder = "")
{
regRead, save_folder, HKU, % lightshot_subkey, LastSavedDir
if (save_folder = "")
save_folder := a_myDocuments "\Lightshot"
}
return save_folder
}
shellMessage(wParam, lParam) {
global ls_id
If (wParam = 1) ; HSHELL_WINDOWCREATED
{
winGet, process_name, ProcessName, % "ahk_id " ls_id := lParam
if (process_name = "Lightshot.exe")
setTimer, set_edit_text, 100
}
}
set_edit_text:
if winExist("Save As ahk_exe Lightshot.exe")
{
setTimer, set_edit_text, off
formatTime, formatted_time, % a_now, % time_format
controlSetText, edit1, % prefix . formatted_time . file_ext(), ahk_class #32770
winWaitClose, Save As ahk_exe Lightshot.exe
goSub, rename_with_date ; rename any other 'screenshot_1.png' files
save_folder := ""
setTimer, set_edit_text, on
}
if !winExist("ahk_id " ls_id)
setTimer, set_edit_text, off
return
custom_menu: ; right click menu
menu, lightshot, useErrorLevel
regRead, keep_selection, HKU, % lightshot_subkey, KeepSelection
regRead, capture_cursor, HKU, % lightshot_subkey, CaptureCursor
save_folder := save_folder()
if (save_folder)
{
stringGetPos, pos, save_folder, \, R2
stringMid, parent_folder, save_folder, % pos + 1
menu, folder_menu, add, % save_folder, set_folder
menu, folder_menu, disable, % save_folder
menu, folder_menu, add
}
folders_created := ""
loop,
{
iniRead, folder, % a_lineFile, folders, folder_%a_index%
if (fileExist(folder)) and (folder != save_folder)
{
menu, folder_menu, add, % folder, set_folder
++folders_created
}
}
until (folder = "ERROR")
if (folders_created)
menu, folder_menu, add
menu, folder_menu, add, Add New Folder, add_folder
menu, lightshot, add, Upload to prntscr.com %a_tab% Ctrl+D, menu_ctrl
menu, lightshot, add, Print %a_tab% Ctrl+P, menu_ctrl
menu, lightshot, add, Copy %a_tab% Ctrl+C, menu_ctrl
menu, lightshot, add, Save %a_tab% Ctrl+S, menu_ctrl
menu, lightshot, add, Select Full Screen %a_tab% Ctrl+A, menu_ctrl
menu, lightshot, add, Clear selection , menu_clear
menu, lightshot, add, Cancel %a_tab% Esc, menu_esc
menu, lightshot, add
menu, lightshot, add, Settings: , menu_close
menu, lightshot, add
menu, lightshot, add, Remember Selection , keep_selection
menu, lightshot, add, Capture Cursor , capture_cursor
menu, lightshot, add
if (save_folder)
menu, lightshot, add, %parent_folder% , menu_close
menu, lightshot, add, Folders , :folder_menu
menu, lightshot, add
menu, lightshot, add, Close this menu , menu_close
menu, lightshot, disable, Settings:
menu, lightshot, disable, %parent_folder%
menu, lightshot, % (keep_selection ? "check" : "unCheck"), Remember Selection
menu, lightshot, % (capture_cursor ? "check" : "unCheck"), Capture Cursor
menu, lightshot, show
menu, lightshot, delete
menu, folder_menu, delete
goSub, rename_with_date ; check for new screenshots
return
menu_ctrl:
letter := subStr(a_thisMenuItem, 0, 1)
send % "^{" format("{:L}", letter) "}"
return
menu_clear:
send {rButton}{down 6} ; select from original menu
sleep 50
send {enter}
return
menu_esc:
send {esc}
return
capture_cursor:
new_value := (capture_cursor = 0) ? 1 : 0
regWrite, REG_DWORD, HKU, % lightshot_subkey, CaptureCursor, % new_value
return
keep_selection:
new_value := (keep_selection = 0) ? 1 : 0
regWrite, REG_DWORD, HKU, % lightshot_subkey, KeepSelection, % new_value
return
menu_close:
return
set_folder:
regWrite, REG_SZ, HKU, % lightshot_subkey, LastSavedDir, % a_thisMenuItem
return
add_folder:
send {esc}
fileSelectFolder, new_folder, *%save_folder%
if (new_folder)
{
loop,
{
iniRead, folder, % a_lineFile, folders, folder_%a_index%
if (folder = "") or (folder = "ERROR")
iniWrite, % " " new_folder, % a_lineFile, folders, folder_%a_index%
}
until (folder = "") or (folder = "ERROR") or (new_folder = folder)
}
return
@davebrny
Copy link
Author

davebrny commented Nov 15, 2017

lightshot+

date filenames

lightshot saves files as 'screenshot_1, screenshot_2' etc which doesnt work well in most cases since changing the save folder to another one that already contains a 'screenshot_1' file means you will get the 'file already exists' warning and have to manually rename it.

this script renames files with a date instead to avoid this problem. the default is "LS_20171115084024.png" but the format can easily be modified to something more readable by adding punctuation:

format result
yyyyMMddhhmmss 20171115084024
yyyy_MM_dd - hh.mm.ss 2017_11_15 - 08.47.24
  • add the format you want to use to the time_format variable
  • if you dont want any prefix before the date then clear or delete the prefix variable
  • add the hotkeys you use for fullscreen screenshots here so that files are renamed when these are used
    • hotkey example: ^!+#p   (ctrl + alt + shift + windows + p)

after a file is saved, any other 'screenshot_x.png' files that were saved before you started using this script will be renamed to match the chosen time format

see the docs page for more info on the various date formats that are available

enter to save

when the screenshot selection window is open you can use the enter key (instead of the default ctrl + s) to quickly save the screenshot to the last folder location without opening up the 'save as' dialog

custom menu

use the L key when at the screenshot selection window to show a custom menu with the following options:

  • toggle whether the selection position is remembered
  • toggle whether the cursor is captured with the screenshot
  • see the name of the current folder where screenshots will be saved to
  • show a list of favourite folders with the option to add a new one
  • all the other options from the default right click menu

the menu can be shown on right click but its disabled since the default menu still pops up every now and again for some reason

favourite folders

multiple folders can be added to the ini section at the top of the file.

increment the number for each key name (folder_3, folder_4 etc) and reload the script to update the changes
(new key names are added automatically when you use the "add new folder" options from the menu)

to set one of these folders as the default save location, select it from the 'folders' list in the custom menu

@log-out
Copy link

log-out commented Mar 15, 2020

Hi thanks, wonderful improvement, could you please explain how can i use this file to enhance the features.

@jouki
Copy link

jouki commented Jun 2, 2020

Hi thanks, wonderful improvement, could you please explain how can I use this file to enhance the features.

You need to AutoHotkey program in order to launch script

@jouki
Copy link

jouki commented Jun 2, 2020

@davebrny Cool script, however, capturing a cursor doesn't work :(

@davebrny
Copy link
Author

davebrny commented Jun 5, 2020

@davebrny Cool script, however, capturing a cursor doesn't work :(

i havnt been using lightshot for a while now but this script just toggles the registry setting for that option

@dpi0
Copy link

dpi0 commented Jul 15, 2020

fantastic work, this really overcomes most of lighshot's weaknesses in terms of functionality

@davebrny
Copy link
Author

@ditdivdotdot i stopped using lightshot a few years ago now but im taking it that they still havnt fixed the "screenshot_1" naming problem? thats sad if true, but im glad to hear this script is still of some use to someone!

@dpi0
Copy link

dpi0 commented Jul 19, 2020

@davebrny nope they still haven't fixed that "screenshot_1" naming problem unfortunately

@S-hdyn
Copy link

S-hdyn commented May 17, 2021

Thanks for this script. This work perfectly but unfortunately there's some errors.

  1. Enter button didn't work
  2. File named Screenshot_x.png when using ctrl+s instead of enter coz this button didn't work
    Could you fix this? Thank you in advance. :)

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