AutoHotkey Community

It is currently May 27th, 2012, 9:50 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 14 posts ] 
Author Message
PostPosted: November 10th, 2006, 7:52 am 
Offline
User avatar

Joined: August 30th, 2005, 8:43 pm
Posts: 8667
Location: Salem, MA
While browsing around the forum, I decided i needed the following script, so I wrote it quickly. you may want to change the keys or add it to your base script or whatever. I have new features in mind, but I wanted to post what I have.

Code:
#SingleInstance
;takes any highlighted text and creates a temporary script from it
;This lets you select cool functions from the AHK forum and run them right away
;It also should let you run text from any program.
;
;By Branden Gunn - engunneer _AT_ gmail _DOT_ com


;Keys:
;F12: run highlighted text as an ahk script
;Shift F12: Save the highlighted text and open
;   it in your default editor - when the editor
;   is closed, the script will start
;Alt F12: Open a dialog box to save the last temporary script to somewhere else
;   this will automatically add an ahk extension if you forget to do it yourself

F12::    ;run highlighted text
  clipboardsaver = ClipboardAll
  Send, ^c
  ifExist temp.ahk
    Filedelete temp.ahk
  FileAppend, %Clipboard%`n, temp.ahk
  Clipboard = clipboardsaver
  clipboardsaver =
  run, temp.ahk
Return

+F12::  ;edit then run
  clipboardsaver = ClipboardAll
  Send, ^c
  ifExist temp.ahk
    Filedelete temp.ahk
  FileAppend, %Clipboard%`n, temp.ahk
  Clipboard = clipboardsaver
  clipboardsaver =
  runwait, edit temp.ahk
  run, temp.ahk
Return

!F12::   ;save last script
IfExist temp.ahk
{
  ;ask user where to copy temp.ahk to
  FileSelectFile, destination, S16, , Where do you want to place this awesome script?, Autohotkey Scripts (*.ahk)
  If ERRORLEVEL
    Msgbox, Script not saved
  Else
  {
    IfNotInString, destination, .ahk
      destination = %destination%.ahk   
    FileCopy, temp.ahk, %destination%, 1
  }
}
Return


Features:
- does not kill your clipboard

Planned features if anyone wants to give input (or help!):
- Way to change hotkey (don't know how to do this yet, but I've seen other scripts do it)
- A way to append it to another script (for collecting scripts into one file)
- A way to kill the temp script

comments welcome!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 10th, 2006, 8:27 am 
Offline

Joined: November 13th, 2004, 4:08 am
Posts: 2951
Location: Minnesota
Just remember to look at code before you run it.. :wink:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 10th, 2006, 9:21 am 
Offline

Joined: December 27th, 2005, 1:46 pm
Posts: 6837
Location: France (near Paris)
Mmm, I am disappointed, I expected a browser plug-in... ;-)

_________________
Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")


Report this post
Top
 Profile  
Reply with quote  
PostPosted: November 10th, 2006, 4:16 pm 
Offline

Joined: May 2nd, 2006, 11:16 pm
Posts: 800
Location: Greeley, CO
engunneer wrote:
While browsing around the forum, I decided i needed the following script...


Funny... I had decided the same thing and started to write a similar script last weekend.

I'll just use yours for a while and see how it progresses.

Something from mine that you might want to incorporate in to yours... When Saving, it captures the URL of the script and adds it as a comment at the beginning of the script for future reference.

_________________
Image
SoggyDog
Dwarf Fortress:
"The most intriguing game I've ever played."


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 10th, 2006, 6:20 pm 
Offline

Joined: August 8th, 2006, 3:55 pm
Posts: 49
See also from garry (24.09.2005)
http://www.autohotkey.com/forum/viewtopic.php?p=33368#33368


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 11th, 2006, 6:51 am 
Offline

Joined: May 2nd, 2006, 11:16 pm
Posts: 800
Location: Greeley, CO
I liked the flow of your script better than what I had done, so I added a few lines to accomodate my need to capture the URL (and a couple other tiny mods to suit my needs) and this is what I got.

Code:
#SingleInstance
#NoTrayIcon
SetWorkingDir, %A_Temp%
ClipSaved =
ScriptText =

F12::      ; F12 only: Run highlighted text
  ControlGetText, URL, Edit1, ahk_class IEFrame
  ClipSaved := ClipboardAll
  Send, ^c
  ifExist AHKscript.ahk
    FileDelete AHKscript.ahk
  ScriptText = `; %URL%`r`n`; `r`n%Clipboard%
  FileAppend, %ScriptText%`r`n, AHKscript.ahk
  Clipboard := ClipSaved
  ClipSaved =
  ScriptText =
  run, AHKscript.ahk
Return

+F12::     ; Shift+F12: Edit then run
  ControlGetText, URL, Edit1, ahk_class IEFrame
  ClipSaved := ClipboardAll
  Send, ^c
  ifExist AHKscript.ahk
    FileDelete AHKscript.ahk
  ScriptText = `; %URL%`r`n`; `r`n%Clipboard%
  FileAppend, %ScriptText%`r`n, AHKscript.ahk
  Clipboard := ClipSaved
  ClipSaved =
  ScriptText =
  runwait, edit AHKscript.ahk
  run, AHKscript.ahk
Return

!F12::     ; Alt+F12: Save last script
IfExist AHKscript.ahk
{
  FileSelectFile, destination, S16, %A_Desktop%, Save Script:, Autohotkey Scripts (*.ahk)
  If ERRORLEVEL
    Msgbox, Script not saved.
  Else
  {
    IfNotInString, destination, .ahk
      destination = %destination%.ahk   
    FileCopy, AHKscript.ahk, %destination%, 1
  }
}
Return

_________________
Image
SoggyDog
Dwarf Fortress:
"The most intriguing game I've ever played."


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 11th, 2006, 10:06 pm 
Offline
User avatar

Joined: August 30th, 2005, 8:43 pm
Posts: 8667
Location: Salem, MA
And you fixed some of my bugs :-)

_________________
Image
(Common Answers) - New Tutorials Forum - Humongous FAQ


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 12th, 2006, 8:50 am 
Offline

Joined: May 2nd, 2006, 11:16 pm
Posts: 800
Location: Greeley, CO
@engunneer:
I didn't mean to take over your thread, but I wanted to share an addition to the script...

Two Emergency Exits (of sorts) were added...

- One allows you to restart (Reload) this script if something goes wrong.
- It's probably not needed, though.

- The other is added to the script being captured in case you can't get out of it any other way.
- This one can come in handy.

Code:
#SingleInstance
#NoTrayIcon
SetWorkingDir, %A_Temp%
ClipSaved =
Emergency_Exit = ^#!q::ExitApp `; DELETE - This line added by Script Capture System

F12::      ; F12 only: Run highlighted text
  ControlGetText, URL, Edit1, ahk_class IEFrame
  ClipSaved := ClipboardAll
  Send, ^c
  ifExist AHKscript.ahk
    FileDelete AHKscript.ahk
  FileAppend, `; %URL%`r`n`; `r`n%Clipboard%`r`n`r`n%Emergency_Exit%`r`n, AHKscript.ahk
  Clipboard := ClipSaved
  ClipSaved =
  run, AHKscript.ahk
Return

+F12::     ; Shift+F12: Edit then run
  ControlGetText, URL, Edit1, ahk_class IEFrame
  ClipSaved := ClipboardAll
  Send, ^c
  ifExist AHKscript.ahk
    FileDelete AHKscript.ahk
  FileAppend, `; %URL%`r`n`;`r`n%Clipboard%`r`n`r`n%Emergency_Exit%`r`n, AHKscript.ahk
  Clipboard := ClipSaved
  ClipSaved =
  runwait, edit AHKscript.ahk
  run, AHKscript.ahk
Return

!F12::     ; Alt+F12: Save last script
IfExist AHKscript.ahk
{
  FileSelectFile, destination, S16, %A_Desktop%, Save Script:, Autohotkey Scripts (*.ahk)
  If ERRORLEVEL
    Msgbox, Script not saved.
  Else
  {
    IfNotInString, destination, .ahk
      destination = %destination%.ahk   
    FileCopy, AHKscript.ahk, %destination%, 1
  }
}
Return

^#!r::   ; CTRL+Win+Alt+r: Reset THIS script if something goes wrong
Reload
Sleep, 5000
MsgBox, Unable to Reload Script
Return


Further, can anyone suggest optimization that would make this script prettier to look at?

_________________
Image
SoggyDog
Dwarf Fortress:
"The most intriguing game I've ever played."


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 12th, 2006, 11:19 pm 
Offline
User avatar

Joined: December 20th, 2004, 12:19 pm
Posts: 798
Location: LooseChange911.com Ask Questions, Demand Answers █ The WTC bldgs █ shouldn't have fallen █ that fast
jonny wrote:
Just remember to look at code before you run it.. :wink:

...YES...my version...
  • Checks for unsafe AHK commands
  • Adds forum title & url to top of script as comment
  • Can add any code before & after the tested script (for emergency exit hotkey, etc...)
  • Prompts before ever running the script & after editing
  • Prompts to delete temp script on exit
  • Can change hotkey in top of script
...I still have more to edit on it, but this is the current version...
...keep checking for it to change...

_________________
AutoHotkey-Hotstring.ahk - Helping the world spell "AutoHotkey" correctly! (btw, it's a lowercase k!)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 13th, 2006, 4:52 am 
Offline
User avatar

Joined: August 30th, 2005, 8:43 pm
Posts: 8667
Location: Salem, MA
Nice! I really like the warning if a potentially dangerous command will be run.

I have two problems, though
1) when it grabbed the URL, it ended up grabbing the URL of the first tab in the firefox window, which was unrelated to the script forum. Not a big deal, and I don't know how to get the correct info from it (Auto It spy doesn't see anything except the first tab's URL in the Window text)

2) There is a very long message box with the code of the script. Again, I can't think of a good way to show the script without a large dialog, so for now, I have commented out

Code:
msgbox, 20, ,
(LTrim
   -------------------
   %TempScript%
   -------------------

   Run script now?
)



Otherwise your script is very useful (and running n my computer now!)

Thanks,

_________________
Image
(Common Answers) - New Tutorials Forum - Humongous FAQ


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 13th, 2006, 6:19 am 
Offline
User avatar

Joined: December 20th, 2004, 12:19 pm
Posts: 798
Location: LooseChange911.com Ask Questions, Demand Answers █ The WTC bldgs █ shouldn't have fallen █ that fast
engunneer wrote:
1) when it grabbed the URL, it ended up grabbing the URL of the first tab in the firefox window...

...ok, I only tested in Netscape 7.2 & IE 6.0...I still have some tricks to try...the next version will try harder to get the correct url...

engunneer wrote:
2) There is a very long message box with the code of the script.

...yes, in my Planned Features I mentioned GUI...I wanna replace that with a GUI & scrolling edit box...next version...

engunneer wrote:
...so for now, I have commented out...

...that msgbox asks a question...how did you comment it out without the answer always being no? You could change it to...

Code:
msgbox, 20, ,
(LTrim
   Run script now?
)

...or...

Code:
StringLeft, TempScript_view, TempScript, 3019
msgbox, 20, ,
(LTrim
   -------------------
   %TempScript_view%
   -------------------

   Run script now?
)
TempScript_view=

engunneer wrote:
Otherwise your script is very useful (and running n my computer now!)

...cool!

_________________
AutoHotkey-Hotstring.ahk - Helping the world spell "AutoHotkey" correctly! (btw, it's a lowercase k!)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 4th, 2006, 4:35 pm 
Offline

Joined: May 2nd, 2006, 11:16 pm
Posts: 800
Location: Greeley, CO
Updated December 4, 2006
- Sometimes I just want to save a script without running it at all,
so I added CTRL+F12 to do that.

Code:
;            =====================================================
;             RUN | EDIT-RUN | SAVE HIGHLIGHTED SCRIPT FROM FORUM
;            =====================================================
;
; Author:    Shannon D Gerton - November 10, 2006; December 4, 2006
;            Code snippets borrowed from Branden Gunn
;            http://www.autohotkey.com/forum/viewtopic.php?p=88842
;
;            ==========
;             Hot Keys
;            ==========
;
; F12:       Run highlighted text as AHKscript.ahk.
;
; Shift+F12: Save highlighted text / open in your default editor.
;            When the editor is closed, the script will start.
;
; Alt+F12:   Open a dialog box to save the last temporary script.
;            Automatically add an ahk extension if you forget to.
;
; CTRL+F12:  Open a dialog box to save the highlighted script.
;            Automatically add an ahk extension if you forget to.
;              --Done WITHOUT running the script at all.
;
; CTRL+Win+Alt+r will Reset THIS script if something goes wrong.
;
; CTRL+Win+Alt+q is added to the copied script as an emergency exit.
;           
;            =====================================================

#SingleInstance
#NoTrayIcon
SetWorkingDir, %A_Temp%
ClipSaved =
Backdoor_Out = ^#!q::ExitApp `; DELETE - This line added by Script Capture System

F12::      ; F12 only: Run highlighted text
  ControlGetText, URL, Edit1, ahk_class IEFrame
  ClipSaved := ClipboardAll
  Send, ^c
  ifExist AHKscript.ahk
    FileDelete AHKscript.ahk
  FileAppend, `; %URL%`r`n`; `r`n%Clipboard%`r`n`r`n%Backdoor_Out%`r`n, AHKscript.ahk
  Clipboard := ClipSaved
  ClipSaved =
  run, AHKscript.ahk
Return

+F12::     ; Shift+F12: Edit then run
  ControlGetText, URL, Edit1, ahk_class IEFrame
  ClipSaved := ClipboardAll
  Send, ^c
  ifExist AHKscript.ahk
    FileDelete AHKscript.ahk
  FileAppend, `; %URL%`r`n`;`r`n%Clipboard%`r`n`r`n%Backdoor_Out%`r`n, AHKscript.ahk
  Clipboard := ClipSaved
  ClipSaved =
  runwait, edit AHKscript.ahk
  run, AHKscript.ahk
Return

!F12::     ; Alt+F12: Save last script
IfExist AHKscript.ahk
{
  FileSelectFile, destination, S16, %A_Desktop%, Save Script:, Autohotkey Scripts (*.ahk)
  If ERRORLEVEL
    Msgbox, Script not saved.
  Else
  {
    IfNotInString, destination, .ahk
      destination = %destination%.ahk   
    FileCopy, AHKscript.ahk, %destination%, 1
  }
}
Return

^F12::     ; CTRL+F12: Save only
ControlGetText, URL, Edit1, ahk_class IEFrame
  ClipSaved := ClipboardAll
  Send, ^c
  ifExist AHKscript.ahk
    FileDelete AHKscript.ahk
  FileAppend, `; %URL%`r`n`; `r`n%Clipboard%`r`n`r`n%Backdoor_Out%`r`n, AHKscript.ahk
  Clipboard := ClipSaved
  ClipSaved =
  IfExist AHKscript.ahk
    {
    FileSelectFile, destination, S16, %A_Desktop%, Save Script:, Autohotkey Scripts (*.ahk)
    If ERRORLEVEL
      Msgbox, Script not saved.
    Else
    {
      IfNotInString, destination, .ahk
        destination = %destination%.ahk   
      FileCopy, AHKscript.ahk, %destination%, 1
    }
  }
Return

^#!r::   ; CTRL+Win+Alt+r: Reset THIS script if something goes wrong
Reload
Sleep, 5000
MsgBox, Unable to Reload Script
Return

_________________
Image
SoggyDog
Dwarf Fortress:
"The most intriguing game I've ever played."


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 10th, 2006, 12:40 pm 
Offline

Joined: November 27th, 2006, 7:41 am
Posts: 222
Location: Queensland, Australia
engunneer wrote:
1) when it grabbed the URL, it ended up grabbing the URL of the first tab in the firefox window, which was unrelated to the script forum.


Try F4 / F6
Here's the code i'm using:
Code:
^+c::
 SetWorkingDir, D:\My Documents\Settings\AutoHotkey\temp\
 clipboard =
 sleep 200
 Send, ^c
 ;clipboard = %clipboard%
 sleep 200
 script = %clipboard%
 SetTitleMatchMode, 2
 IfWinActive,Mozilla Firefox
 {
  soundbeep, 1000, 100
  Send {F6}
  Send {CTRL Down}c{CTRL Up}     
  Send {F6} 
  sleep 200
  URL = %clipboard%
 }
 IfWinActive, Internet Explorer
 {
  soundbeep, 2000, 100
  sleep 100
  soundbeep, 2000, 100
  Send {F4}
  Send, ^c     
  Send {F4} 
  sleep 200
  URL = %clipboard%
 }
 WinGetActiveTitle, App
 
 header1 = `;`n`; Automatically generated script`r`n`;`n
 header2 = #NoEnv`r`n#singleinstance force`r`nsendmode Input`r`n`n`;---- copied script body ----`r`n`n
 scriptname = %A_Now%.ahk
 FileAppend, %header1%`; ..Copied from:`r`n;   %URL%`r`n`;   %App%`r`n`;   On: %A_YYYY%.%A_MM%.%A_DD% %A_HOUR%:%A_MIN%:%A_SEC%`r`n`;`r`n%header2%%script%`r`n, %scriptname%
 sleep 500
 run, %scriptname%
 run, edit %scriptname%
return
/* example - select the following line and hit ^+c
msgbox, %A_ScriptDir%\%A_ScriptName%
*/


and the output is:
Code:
;
; Automatically generated script
;
; ..Copied from:
;   http://www.autohotkey.com/forum/viewtopic.php?p=93944#93944
;   run scripts directly from the forum! - Mozilla Firefox
;   On: 2006.12.13 06:16:28
;
#NoEnv
#singleinstance force
sendmode Input

;---- copied script body ----

msgbox, %A_ScriptDir%\%A_ScriptName%


...not ideal, and YES you have to manually delete some files occasionally, but it works ok.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 16th, 2010, 2:16 am 
Offline

Joined: October 7th, 2006, 4:50 pm
Posts: 3157
Location: MN, USA
All of these examples, as well as FSC - Forum Script Copier [BoBo], require the user to press a hotkey. It may be simpler and easier to use OnClipboardChange.
Code:
Gui, Add, Edit,% "vEdit w" A_ScreenWidth//1.5 " h" A_ScreenHeight//2
Gui, Add, Button, Default, Save
Gui, Add, Button, x+20 wp, Run
return

OnClipboardChange:
 WinWaitActive, ahk_class #32770, Code copied to clipboard., 5
 If ErrorLevel
  return
 WinClose
 WinGetActiveTitle, title
 GuiControl,,Edit, %Clipboard%
 Gui, Show
return

ButtonRun:
ButtonSave:
 Gui, Submit
 Loop {
  num := A_Index=1 ? "":" (" A_Index ")"
  path := A_Desktop "\" title num ".ahk"
  IfNotExist, %path%
   break
 }
 FileAppend, %Edit%, %path%

 If A_ThisLabel = ButtonRun
  Run, %path%
return


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: sks, Yahoo [Bot] and 22 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group