AutoHotkey: Make shortcut file from clipboard contents: Difference between revisions

From TotalcmdWiki
Jump to navigation Jump to search
(Added category AutoHotkey scripts)
(V1.22, support for more complex URLs, file name in parameters)
 
(4 intermediate revisions by the same user not shown)
Line 15: Line 15:
<!--  
<!--  
*/ ; --> <code><pre>
*/ ; --> <code><pre>
; ////////////////////////////////////////////////////////////////
; /////////////////////////////////////////////////////////////////////
; // MkNewShortcut_from_Clipboard.ahk v1.01 -  (W) StatusQuo 2008
; // MkNewShortcut_from_Clipboard.ahk v1.22 -  (W) StatusQuo 2008-2021
; ////////////////////////////////////////////////////////////////
; /////////////////////////////////////////////////////////////////////
; // Make new shortcut (lnk/URL) using actual clipboard information.
; // Make new shortcut (lnk/URL) using actual clipboard information.
; // Tested on Win2k SP4, supposed to run well on later versions.
; // Tested on Win2k SP4 (v1.01) and Win7 (Update v1.10..v1.22).
; // Probably not working on WinVista: MS removed the NewLinkHere function and put it back in Win7.
; //
; //
; // Thanks to icfu for pointing me to appwiz.cpl function
; // Thanks to icfu for pointing me to appwiz.cpl function
; ////////////////////////////////////////////////////////////////
; /////////////////////////////////////////////////////////////////////


#SingleInstance, ignore
#NoEnv
#Warn
boDebug := "0"
sRunCmd = rundll32.exe appwiz.cpl`,NewLinkHere %A_WorkingDir%\
sRunCmd = rundll32.exe appwiz.cpl`,NewLinkHere %A_WorkingDir%\
sURL   = %Clipboard%
sWinTitleNewLinkW7  := "ahk_class NativeHWNDHost"
sWinTitleNewLinkW2k := "ahk_class #32770"
sTempFileName      := A_WorkingDir . "\" . A_Now . "_" . A_ScriptName . ".tmp"
if not (boDebug)
sURL := Clipboard
;
; *** W2k
DlgUrlEdit  := "Edit1"
DlgUrlBut  := "Button3"
DlgFileEdit := "Edit1"
;DlgFileBut  := ; *** using manual Enter!
; *** W7
if A_OSVersion in WIN_95,WIN_98,WIN_ME,WIN_NT4,WIN_2000,WIN_XP ; *** WIN_VISTA: unsupported by MS tool; Win_7: new syntax for MS tool
{
} else {
FileAppend,, %sTempFileName%
sRunCmd = rundll32.exe appwiz.cpl`,NewLinkHere %sTempFileName%
DlgUrlEdit  := "Edit1"
DlgUrlBut  := "Button1"
DlgFileEdit := "Edit2"
;DlgFileBut  := "Button2" ; *** using manual Enter!
}


Run, %sRunCmd%
Run, %sRunCmd%
WinWaitActive, ahk_class #32770,,3
Loop, 12
if Errorlevel
{
{ MsgBox, *** Error: `n`nWaiting for CreateLink-Window timed out.
Sleep, 300
Exit
IfWinActive, %sWinTitleNewLinkW2k% ; *** Win2k
Break
IfWinActive, %sWinTitleNewLinkW7% ; *** Win7
Break
}
}
ControlSetText, Edit1, %sURL%
IfWinNotActive, %sWinTitleNewLinkW7%
; ControlSend, Edit1, {END} ; jump to end, showing file name to user (+ emulating real input behaviour)
IfWinNotActive, %sWinTitleNewLinkW2k%
Sleep, 250 ; let user get a quick look at the URL
{
ControlClick,  Button3 ; ControlSend,    Edit1, {ENTER}
MsgBox, *** Error: `n`nWaiting for CreateLink-Window timed out.
ExitApp
}
ControlSetText, %DlgUrlEdit%, %sURL%
Sleep, 250 ; *** let user get a quick look at the URL
ControlClick,  %DlgUrlBut% ; ControlSend,    %DlgUrlEdit%, {ENTER}
Sleep, 250 ; *** time to display 2nd window (target file name)


sFileName = %Clipboard%
sFileName := sURL ; %Clipboard%
{ ; Clipboard = Internet URL
{ ; Clipboard = Internet or local URL
; StringGetPos: zero-based, first char is #0
; StringGetPos: zero-based, first char is #0
StringGetPos, iPos, sFileName, /, R
; InStr()    :  One-based, first char is #1
if (iPos < 0)
; ---------
{ ; Clipboard = local path
; *** trim left of file name, after last (back)slash
StringGetPos, iPos, sFileName, \, R
StringGetPos, iPosLastSep, sFileName, /, R ; *** 0=first char, -1=not found
if (iPosLastSep < 0) ; *** Clipboard = local path
{
StringGetPos, iPosLastSep, sFileName, \, R ; *** 0=first char, -1=not found
}
if (iPosLastSep < 0) ; *** Clipboard = useless
{
Exit
}
}
if (iPos < 0)
if (iPosLastSep + 1 >= StrLen(sFileName)) ; *** last char is separator/(back)slash, no filename found in clipboard
{ ; Clipboard = useless
{
Exit
Exit
} else {
StringMid, sFileName, sFileName, iPosLastSep + 2 ; *** start after found separator
iPosLastSep = -1 ; *** 0=PosName, -1=virtual separator pos after all have been removed
}
; ---------
; *** trim left of file name, after param separator
if (SubStr(sFileName, 1, 1) = "?") ; *** parameters first, filename in one of parameters (last?)
{
sFileName := SubStr(sFileName, 2) ; *** remove question mark before file name/parameters
;
if (iPosLastSep < 0) ; *** Clipboard = FN param in long URL
{ ; https://base.tld/redirect/?a=name&s=s&d=pa&f=file.exe&xy=text - any pos, param name "f"
StringGetPos, iPosLastSep, sFileName, f=, R ; *** 0=first char, -1=not found
if (iPosLastSep >= 0)
iPosLastSep := iPosLastSep + 1 ; *** "f=": skip 2 chars instead of 1
}
if (iPosLastSep < 0) ; *** Clipboard = FN param in long URL
{ ; https://base.tld/redirect/?a=name&s=s&d=pa&xy=file.exe        - end pos, param name undefined
StringGetPos, iPosLastSep, sFileName, =, R ; *** 0=first char, -1=not found
}
}
; ---------
; *** trim right
if (InStr(sFileName, "?") > 0) ; *** remove trailing parameter from URL/filename
{
sFileName := (SubStr(sFileName, 1, InStr(sFileName, "?") - 1))
;Goto FnExtractDone
}
}
if (iPos + 1 >= StrLen(sFileName))
; ---------
{ ; last char is (back)slash, no filename found in clipboard
; *** trim right
if (iPosLastSep + 1 >= StrLen(sFileName)) ; *** last char is separator, no filename found in clipboard
{
Exit
Exit
} else {
} else {
StringMid, sFileName, sFileName, iPos + 2
StringMid, sFileName, sFileName, iPosLastSep + 2 ; *** start after found separator (iPosLastSep=-1 if no separator existing, then no change)
}
if (InStr(sFileName, "&") > 1) ; *** cut additional trailing parameters
{
sFileName := SubStr(sFileName, 1, InStr(sFileName, "&") - 1)
}
}
}
}
FnExtractDone:
While (InStr(sFileName, "%20") > 0) ; *** remove HTML hex codes from URL/filename
{
sFileName := (SubStr(sFileName, 1, InStr(sFileName, "%20") - 1)) . " " . (SubStr(sFileName, InStr(sFileName, "%20") + 3))
}
if (FileExist(sFileName . ".url"))
sFileName := sFileName . "_from"


;- wait for system to process the link and proceed to link file name
;- wait for system to process the link and proceed to link file name
;- check, if system proceeded to second window, asking for link file name;  if not: wait
;- check, if system proceeded to second window, asking for link file name;  if not: wait
Loop, 15
Loop, 15
{ ControlGetText, sTmp, Edit1
{
ControlGetText, sTmp, %DlgFileEdit%
if sTmp!=%sURL%
if sTmp!=%sURL%
Break
Break
Line 71: Line 157:
}
}


ControlSetText, Edit1, %sFileName% ; ControlSendRaw, Edit1, %sFileName%
ControlSetText, %DlgFileEdit%, %sFileName% ; ControlSendRaw, %DlgFileEdit%, %sFileName%
ControlSend  , Edit1, {END} ; let cursor jump to end, emulating real input behaviour
ControlSend  , %DlgFileEdit%, {END} ; let cursor jump to end, emulating real input behaviour
;</pre></code>Back to [[AutoHotkey]]
Return
 
;</pre></code>
;<!--
/*
-->
Back to [[AutoHotkey]]
 
[[Category:AutoHotkey scripts|Make shortcut file from clipboard contents]]
[[Category:AutoHotkey scripts|Make shortcut file from clipboard contents]]
<!--
*/ ; -->

Latest revision as of 03:34, 12 September 2021

Make a new shortcut (*.LNK for local targets, *.URL for internet URLs),

-taking the clipboard's contents as target address and
-trying to auto-detect the file name for *.lnk/*.url

Leaves only the last RETURN key press to the user, the rest is automated.


Related links:


; /////////////////////////////////////////////////////////////////////
; // MkNewShortcut_from_Clipboard.ahk v1.22  -  (W) StatusQuo 2008-2021
; /////////////////////////////////////////////////////////////////////
; // Make new shortcut (lnk/URL) using actual clipboard information.
; // Tested on Win2k SP4 (v1.01) and Win7 (Update v1.10..v1.22).
; // Probably not working on WinVista: MS removed the NewLinkHere function and put it back in Win7.
; //
; // Thanks to icfu for pointing me to appwiz.cpl function
; /////////////////////////////////////////////////////////////////////

#SingleInstance, ignore
#NoEnv
#Warn

boDebug := "0"
sRunCmd = rundll32.exe appwiz.cpl`,NewLinkHere %A_WorkingDir%\
sWinTitleNewLinkW7  := "ahk_class NativeHWNDHost"
sWinTitleNewLinkW2k := "ahk_class #32770"
sTempFileName       := A_WorkingDir . "\" . A_Now . "_" . A_ScriptName . ".tmp"
if not (boDebug)
	sURL := Clipboard
;
; *** W2k
DlgUrlEdit  := "Edit1"
DlgUrlBut   := "Button3"
DlgFileEdit := "Edit1"
;DlgFileBut  := 	; *** using manual Enter!
; *** W7
if A_OSVersion in WIN_95,WIN_98,WIN_ME,WIN_NT4,WIN_2000,WIN_XP	; *** WIN_VISTA: unsupported by MS tool; Win_7: new syntax for MS tool
{
} else {
	FileAppend,, %sTempFileName%
	sRunCmd = rundll32.exe appwiz.cpl`,NewLinkHere %sTempFileName%
	DlgUrlEdit  := "Edit1"
	DlgUrlBut   := "Button1"
	DlgFileEdit := "Edit2"
	;DlgFileBut  := "Button2"	; *** using manual Enter!
}

Run, %sRunCmd%
Loop, 12
{
	Sleep, 300
	IfWinActive, %sWinTitleNewLinkW2k%	; *** Win2k
		Break
	IfWinActive, %sWinTitleNewLinkW7%	; *** Win7
		Break
}
IfWinNotActive, %sWinTitleNewLinkW7%
	IfWinNotActive, %sWinTitleNewLinkW2k%
	{
		MsgBox, *** Error: `n`nWaiting for CreateLink-Window timed out.
		ExitApp
	}
ControlSetText, %DlgUrlEdit%, %sURL%
Sleep, 250	; *** let user get a quick look at the URL
ControlClick,   %DlgUrlBut%	; ControlSend,    %DlgUrlEdit%, {ENTER}
Sleep, 250	; *** time to display 2nd window (target file name)

sFileName := sURL	; %Clipboard%
{		; Clipboard = Internet or local URL
		; StringGetPos: zero-based, first char is #0
		; InStr()     :  One-based, first char is #1
		; ---------
		; *** trim left of file name, after last (back)slash
	StringGetPos, iPosLastSep, sFileName, /, R	; *** 0=first char, -1=not found
	if (iPosLastSep < 0)	; *** Clipboard = local path
	{
		StringGetPos, iPosLastSep, sFileName, \, R	; *** 0=first char, -1=not found
	}
	if (iPosLastSep < 0)	; *** Clipboard = useless
	{
		Exit
	}
	if (iPosLastSep + 1 >= StrLen(sFileName))	; *** last char is separator/(back)slash, no filename found in clipboard
	{
		Exit
	} else {
		StringMid, sFileName, sFileName, iPosLastSep + 2	; *** start after found separator
		iPosLastSep = -1	; *** 0=PosName, -1=virtual separator pos after all have been removed
	}
		; ---------
		; *** trim left of file name, after param separator
	if (SubStr(sFileName, 1, 1) = "?")	; *** parameters first, filename in one of parameters (last?)
	{
		sFileName := SubStr(sFileName, 2)	; *** remove question mark before file name/parameters
		;
		if (iPosLastSep < 0)	; *** Clipboard = FN param in long URL
		{		; https://base.tld/redirect/?a=name&s=s&d=pa&f=file.exe&xy=text - any pos, param name "f"
			StringGetPos, iPosLastSep, sFileName, f=, R	; *** 0=first char, -1=not found
			if (iPosLastSep >= 0)
				iPosLastSep := iPosLastSep + 1	; *** "f=": skip 2 chars instead of 1
		}
		if (iPosLastSep < 0)	; *** Clipboard = FN param in long URL
		{		; https://base.tld/redirect/?a=name&s=s&d=pa&xy=file.exe        - end pos, param name undefined
			StringGetPos, iPosLastSep, sFileName, =, R	; *** 0=first char, -1=not found
		}
	}
		; ---------
		; *** trim right
	if (InStr(sFileName, "?") > 0)	; *** remove trailing parameter from URL/filename
	{
		sFileName := (SubStr(sFileName, 1, InStr(sFileName, "?") - 1))
		;Goto FnExtractDone
	}
		; ---------
		; *** trim right
	if (iPosLastSep + 1 >= StrLen(sFileName))	; *** last char is separator, no filename found in clipboard
	{
		Exit
	} else {
		StringMid, sFileName, sFileName, iPosLastSep + 2	; *** start after found separator (iPosLastSep=-1 if no separator existing, then no change)
	}
	if (InStr(sFileName, "&") > 1)	; *** cut additional trailing parameters
	{
		sFileName := SubStr(sFileName, 1, InStr(sFileName, "&") - 1)
	}
}
FnExtractDone:

While (InStr(sFileName, "%20") > 0)	; *** remove HTML hex codes from URL/filename
{
	sFileName := (SubStr(sFileName, 1, InStr(sFileName, "%20") - 1)) . " " . (SubStr(sFileName, InStr(sFileName, "%20") + 3))
}

if (FileExist(sFileName . ".url"))
	sFileName := sFileName . "_from"

	;- wait for system to process the link and proceed to link file name
	;- check, if system proceeded to second window, asking for link file name;  if not: wait
Loop, 15
{	
	ControlGetText, sTmp, %DlgFileEdit%
	if sTmp!=%sURL%
		Break
	Sleep, 100
}
if sTmp=%sURL%
{	MsgBox, *** Error: `n`nFirst CreateLink window still active, aborting.
	Return	; Exit
}

ControlSetText, %DlgFileEdit%, %sFileName%	; ControlSendRaw, %DlgFileEdit%, %sFileName%
ControlSend   , %DlgFileEdit%, {END}	; let cursor jump to end, emulating real input behaviour
Return

;

Back to AutoHotkey