AutoHotkey - Building a Clipboardsaving Function - function

What I want to do is to build a function, that I can use to paste something (in this case URLs). Normally I would just use the send command or the sendinput command, but they are kind of slow which is a little bit annoying. That's why I want to avoid it and use the clipboard instead.
Here my function:
ClipPaster(CustomClip){
ClipSaved := ClipboardAll ;Saving the current clipboard
Clipboard := %CustomClip% ;Overwriting the current clipboard
Send, ^{v}{Enter} ;pasting it into the search bar
Clipboard := Clipsaved ;Recovering the old clipboard
}
Here how I'm using the function:
RAlt & b::
Send, ^{t} ;Open a new tab
ClipPaster("chrome://settings/content/images") ;Activating my clipboard
return
RAlt & g::
Send, ^{t} ;Open a new tab
ClipPaster("https://translate.google.com/#en/es/violin") ;Activating
my clipboard function
return
Then when I'm trying to use the function. I get an error:
Error: The following variable name contains an illegal character: "chrome://settings/content/images"
Line:
-->1934: Clipboard := %CustomClip%
What am I doing wrong here?

You get this error message because
Variable names in an expression are NOT enclosed in percent signs.
https://www.autohotkey.com/docs/Variables.htm#Expressions
ClipPaster(CustomClip){
ClipSaved := ClipboardAll ; Saving the current clipboard
Clipboard := "" ; empty the clipboard (start off empty to allow ClipWait to detect when the text has arrived)
; Variable names in an expression are NOT enclosed in percent signs:
Clipboard := CustomClip ; Overwriting the current clipboard
ClipWait 1 ; wait max. 1 second for the clipboard to contain data
if (!ErrorLevel) ; If NOT ErrorLevel clipwait found data on the clipboard
Send, ^v{Enter} ; pasting it into the search bar
Sleep, 300
Clipboard := Clipsaved ; Recovering the old clipboard
ClipSaved := "" ; Free the memory
}
https://www.autohotkey.com/docs/misc/Clipboard.htm#ClipboardAll

Related

Rerun the program after it ends automatically in octave

Using OCTAVE only...
How can I rerun the code automatically after it ends. Like I want to make a program in which if the input is incorrect value it will end the program and rerun again.
I tried that by writting there a file name and it works but this will only work until I change my file name.
You can wrap your main script in a wrapper script which performs the loop.
% In main.m
disp( 'Hello from main' );
Question = "Do you want to rerun? ";
Response = input( Question, 's');
% in wrapper.m
Response = 'yes';
while strcmp( Response, 'yes' )
main
end

Autohot key variable function not global

I'm trying to make a function that takes coordinates, copies that field and name a variable, but the variable seems to be local and doesn't transfer out of the function. The coordinates (x,y) and delay seem to be working fine, but the desc variable always comes out blank. Please help!
newest(x, y, delay, variable)
{
sleep, %delay%
click, %x%,%y% ;desc machine field
clipboard = ; empty the clipboard
Loop
{
If GetKeyState("F2","P") ; terminate the loop whenever you want by pressing F2
break
Send {Ctrl Down}c{Ctrl Up}
if clipboard is not space
break ; Terminate the loop
}
variable := clipboard ;
msgbox %variable%
return
}
^k::
newest(654, 199, 200, desc)
msgbox %desc%
return
From the function's point of view, parameters are essentially the same
as local variables unless they are defined as ByRef.
ByRef makes one parameter of the function an alias for a variable, allowing the function to assign a new value to this variable.
newest(x, y, delay, ByRef variable){
sleep, %delay%
click, %x%,%y% ; desc machine field
clipboard = ; empty the clipboard
Loop
{
If GetKeyState("F2","P") ; terminate the loop whenever you want by pressing F2
break
Send {Ctrl Down}c{Ctrl Up}
If clipboard is not space
break ; Terminate the loop
}
variable := clipboard
msgbox %variable%
return
}
^k::
newest(54, 199, 200, desc)
msgbox %desc%
return

Create HotKey inside a function (AutoHotKey)

So, I want my function to create a HotKey and returns the corresponding text of the hotkey when i press
here is my code below
global Object := {a:[1,"a","alexa"],b:[2,"b","battle"]}
global key_var1 :="!a"
global key_var2 := "!b"
create(key)
{
HotKey, %key%, myKey
return
myKey:
MsgBox, % Object.key[3]
return
}
create(key_var1)
create(key_var2)
The issue here is, when i press the hotkey, the message box displays nothing just empty.
When i press the HotKey the message box must display the corresponding text inside my Object array (text is in position 3)
Displays associative array element keyed to current hotkey:
global Object := {"!a":[1,"a","alexa"], "!b":[2,"b","battle"]}
global key_var1 := "!a"
global key_var2 := "!b"
create(key)
{
HotKey, %key%, myKey
return
myKey:
MsgBox, % A_ThisHotkey ":" Object[A_ThisHotkey][3]
return
}
create(key_var1)
create(key_var2)
Output:

Returning insert mode cursor to same place after function call

I've been trying to write a bit of Vimscript to call a function when I insert the same character twice, in my particular case I wanted that if you inserted semi colon twice for it to actually move the semi colon to the end of the line.
command! Semi call Semi()
inoremap ; <C-O>:Semi2<CR>
function! Semi()
let x = getpos(".")
" If we are in the last column..
if col(".")+1 == col("$")
let insert_semi = getline(".") . ";"
call setline(".", insert_semi)
let x[2] += 1
call setpos(".", x)
return
endif
let char = getline(".")[x[2] - 2]
if char == ";"
" if prev char was a semicolon also, remove and append to the end
else
" insert semicolon normally...
endif
endfunction
The problem I am having is when calling this function on the last column, you have to exit insert mode to call this function the cursor will go into normal mode and move the cursor to the last column. Is there any way to tell whether the cursor was appending to the end of the line or inserting before the last column and when function call is finished return it to the same position?
I am well aware that I could use an insert mapping on ;; however I dislike this behaviour, where Vim goes into a waiting for next key mode and does not display what you have written. This issue is not only to do with my problem listed but a more general problem which also occurs in the first column.
If your function does not use insert mode to append the ';' -- e.g. by pasting from a buffer -- you can use the gicommand to return to the place, where you exited insert mode.
I would advise against using i_CTRL-O, it triggers InsertLeave and InsertEnter events, which may affect other plugins. I would use :inoremap <expr> ; here. See :help :map-expr. Inside that expression (i.e. your function), record the current cursor position and compare it with the last recorded one. If it's next to it, return the keys to undo the inserts and redo at the end (<BS><BS><End>;), else just return ;.
You don't need a function for that:
inoremap ;; ;;<Esc>h"_xxm`$p``a
or cleaner:
inoremap ;; <Esc>m`A;<Esc>``a

Lazarus & Free Pascal - How to recursively copy a source directory of files to another directory?

I need to add some functionality to my Lazarus & Free Pascal GUI program - I need it to also copy files from a users chosen dir to another dir. I have a "Choose Source" TSelectDirectoryDialog button onclick event for the source directory and a "Choose Destination" TSelectDirectoryDialog button onclick event for the destination dir. I have a 3rd button to do the copying from Source to Destination.
So far, I have found CopyFile that copies the files and the original date attributes, but it doesn't recreate the subdirectory structure of any subdirectories of the users chosen source directory. I am, in effect, trying to replicate the source directory in a new directory elsewhere.
I have got this far :
Public Vars :
DestDir, SourceDir : string
...
FS := TFileSearcher.Create;
FS.OnFileFound := #CopyTheFile; // CopyTheFile is my own procedure
FS.Search(SourceDir, '*', True);
...
procedure TForm1.CopyTheFile(FileIterator: TFileIterator);
var
DestinationName: String;
begin
DestinationName := IncludeTrailingPathDelimiter(DestDir) + ExtractFileName(FileIterator.FileName);
if not FileUtil.CopyFile(FileIterator.FileName, DestinationName, true) then
ShowMessage(FileIterator.FileName + ' failed to copy');
end;
Can anyone help me with how to code in copying of subdirectories and their files? I have also asked the question at the Lazarus forum here : Lazarus Thread
Many thanks
Ted
I am VERY HAPPY AND PROUD and to provide, for the first time, an answer to my own question! I stripped the whole thing back to basics and stopped reading other people's more complex examples (because they just confused me). I stuck to the basic procedures listed at Lazarus FileUtils Ref and came up with this, which works. I need to build in some error checking and stuff, but what I now have is code that takes the source directory, rebuilds it in the destination directory and then copies the files from the originating directory to the destination, using entirely Free Pascal code and no OS specific syntax. Pasted below for benefit of others. Please add any contructive comments to make it better, faster, more efficient. Thanks.
procedure TForm1.Button3Click(Sender: TObject);
begin
ProcessDir(SourceDir);
end;
procedure TForm1.ProcessDir(const SourceDirName: string);
var
NoOfFilesFoundInSourceDir, i, NoOfFilesCopiedOK : integer;
FilesFoundToCopy : TStringList;
SourceDirectoryAndFileName, SubDirStructure, FinalisedDestDir, FinalisedFileName : string;
begin
Memo1.Lines.Clear;
SubDirStructure := '';
FinalisedDestDir := '';
NoOfFilesFoundInSourceDir := 0;
NoOfFilesCopiedOK := 0;
// Ensures the selected source directory is set as the directory to be searched
// and then fina all the files and directories within, storing as a StringList.
SetCurrentDir(SourceDirName);
FilesFoundToCopy := FindAllFiles(SourceDirName, '*', True);
NoOfFilesFoundInSourceDir := FilesFoundToCopy.Count;
try
for i := 0 to FilesFoundToCopy.Count -1 do
begin
Memo1.Lines.Add('File Index : '+IntToStr(i)+' File Name: '+FilesFoundToCopy.Strings[i]);
SourceDirectoryAndFileName := ChompPathDelim(CleanAndExpandDirectory(FilesFoundToCopy.Strings[i]));
// Determine the source sub-dir structure, from selected dir downwards
SubDirStructure := IncludeTrailingPathDelimiter(ExtractFileDir(SourceDirectoryAndFileName));
// Now concatenate the original sub directory to the destination directory and form the total path, inc filename
// Note : Only directories containing files will be recreated in destination. Empty dirs are skipped.
// Zero byte files are copied, though, even if the directory contains just one zero byte file.
FinalisedDestDir := DestDir+SubDirStructure;
FinalisedFileName := ExtractFileName(FilesFoundToCopy.Strings[i]);
// Now create the destination directory structure, if it is not yet created. If it exists, just copy the file.
if not DirPathExists(FinalisedDestDir) then
begin
if not ForceDirectories(FinalisedDestDir) then
begin
ShowMessage(FinalisedDestDir+' cannot be created.');
end;
end;
// Now copy the files to the destination dir
if not FileUtil.CopyFile(SourceDirectoryAndFileName, FinalisedDestDir+FinalisedFileName, true) then
begin
ShowMessage('Failed to copy file : ' + SourceDirectoryAndFileName)
end
else
NoOfFilesCopiedOK := NoOfFilesCopiedOK + 1;
end;
finally
FilesFoundToCopy.free;
end;
ShowMessage('Total files copied OK : ' + IntToStr(NoOfFilesCopiedOK));
end;