AutoHotkey: Make shortcut file from clipboard contents: Difference between revisions
Jump to navigation
Jump to search
(Update for Win7: changed syntax and control names of dialog "rundll32.exe appwiz.cpl, NewLinkHere") |
m (re-added category AutoHotkey scripts, better name for temp file) |
||
Line 26: | Line 26: | ||
sRunCmd = rundll32.exe appwiz.cpl`,NewLinkHere %A_WorkingDir%\ | sRunCmd = rundll32.exe appwiz.cpl`,NewLinkHere %A_WorkingDir%\ | ||
sWinTitleNewLinkW7 := "ahk_class NativeHWNDHost" | sWinTitleNewLinkW7 := "ahk_class NativeHWNDHost" | ||
sWinTitleNewLinkW2k := "ahk_class #32770" | sWinTitleNewLinkW2k := "ahk_class #32770" | ||
sTempFileName := A_WorkingDir . "\" . A_Now . "_" . A_ScriptName . ".tmp" | |||
sURL := Clipboard | |||
; | ; | ||
; *** W2k | ; *** W2k | ||
Line 39: | Line 40: | ||
{ | { | ||
} else { | } else { | ||
FileAppend,, % | FileAppend,, %sTempFileName% | ||
sRunCmd = rundll32.exe appwiz.cpl`,NewLinkHere % | sRunCmd = rundll32.exe appwiz.cpl`,NewLinkHere %sTempFileName% | ||
;:MsgBox, debug sRunCmd:`n%sRunCmd% | ;:MsgBox, debug sRunCmd:`n%sRunCmd% | ||
DlgUrlEdit := "Edit1" | DlgUrlEdit := "Edit1" | ||
Line 110: | Line 111: | ||
ControlSetText, %DlgFileEdit%, %sFileName% ; ControlSendRaw, %DlgFileEdit%, %sFileName% | ControlSetText, %DlgFileEdit%, %sFileName% ; ControlSendRaw, %DlgFileEdit%, %sFileName% | ||
ControlSend , %DlgFileEdit%, {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]] | ;</pre></code> | ||
;<!-- | |||
/* | |||
--> | |||
Back to [[AutoHotkey]] | |||
[[Category:AutoHotkey scripts|Make shortcut file from clipboard contents]] | |||
<!-- | |||
*/ ; --> |
Revision as of 13:59, 15 February 2014
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.10 - (W) StatusQuo 2008-2014
; /////////////////////////////////////////////////////////////////////
; // Make new shortcut (lnk/URL) using actual clipboard information.
; // Tested on Win2k SP4 (v1.01) and Win7 (Update v1.10).
; // 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
; /////////////////////////////////////////////////////////////////////
sRunCmd = rundll32.exe appwiz.cpl`,NewLinkHere %A_WorkingDir%\
sWinTitleNewLinkW7 := "ahk_class NativeHWNDHost"
sWinTitleNewLinkW2k := "ahk_class #32770"
sTempFileName := A_WorkingDir . "\" . A_Now . "_" . A_ScriptName . ".tmp"
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%
;:MsgBox, debug sRunCmd:`n%sRunCmd%
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 = %Clipboard%
{ ; Clipboard = Internet URL
; StringGetPos: zero-based, first char is #0
StringGetPos, iPos, sFileName, /, R
if (iPos < 0)
{ ; Clipboard = local path
StringGetPos, iPos, sFileName, \, R
}
if (iPos < 0)
{ ; Clipboard = useless
Exit
}
if (iPos + 1 >= StrLen(sFileName))
{ ; last char is (back)slash, no filename found in clipboard
Exit
} else {
StringMid, sFileName, sFileName, iPos + 2
}
if (InStr(sFileName, "?")>0)
{ ; remove appended parameter from URL/filename
sFileName := (SubStr(sFileName, 1, InStr(sFileName, "?") - 1))
}
}
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
;
Back to AutoHotkey