AutoHotkey: Send clipboard & upload file in google drive

Luis Diaz
Luis Diaz used Ask the Experts™
on
Hello experts,
I set up the following AutoHotkey to send clipboard on google drive and upload files.
+F9::
FilePath1:=Clipboard
FilePath2:=StrReplace(FilePath1, """")
If (Not FileExist(FilePath2))
{
	Msgbox,0x10,Error, Your clipboard which report the following value %FilePath2% doesn't exist	
	Return
}
Else
Msgbox,0x40,Success, Your clipboard which report the following value %FilePath2% exist	
WinGetActiveTitle,Title
If (InStr(Title,"Google Drive"))
{
	PostMessage,0x112,0xF030,,,%Title%
	Sleep, 100
	Send, mc 	
	Sleep, 100
	Send, {Down 2}
	Sleep, 100
	Send, {Enter}
	Sleep, 1500
	WinGetActiveTitle,Title
	If (InStr(Title,"Open"))
	{	
		Send, %FilePath2%
		Sleep, 100
		Send, {Enter}
		Return
	}
	Else
	MsgBox,0x10,Error,Unable to send path as active window doesn't contains name expected
	Return
}
Else
  MsgBox,0x10,Error,Google Drive is not the active window
Return

Open in new window


I made a test with the following cases and it works:
1-Error when clipboard doesn't contain a file which exist
2-Success when the clipboard contain a file which exist
3-Error when google drive is not the active windows

Prior to adding to my AutoHotkey file I would like to have some advice to see if the AutoHotkey should be revised in term of structure, conditions and loops.
I am aware that Send keys is not the best way however this is how I can work on google drive.

Thank you for your help.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Joe WinogradDeveloper
Fellow 2017
Most Valuable Expert 2018
Commented:
revised in term of structure, conditions and loops
Hi Luis,
Comments related to your question above:

(1) There are no loops anywhere in the code.

(2) We've had the "always on top" discussion before. If you want the first, third, and fourth MsgBox (lines 6, 31, 35) on top, change the 0x10 (16) to 0x1010 (4112). If you want the second MsgBox (line 10) on top, change the 0x40 (64) to 0x1040 (4160). The MsgBox numeric options (hex and decimal) are explained here:
https://www.autohotkey.com/docs/commands/MsgBox.htm

(3) We've had the "indentation" discussion before. The second MsgBox (line 10) should be indented. It is the only statement that gets executed after the Else, because there are no braces following the Else (hence, no block of statements). Likewise, the third MsgBox (line 31) should be indented, because there are no braces following the Else, so it is the only statement that gets executed in the Else path.

(4) I don't know if the sequence of Send and Sleep commands after the PostMessage is correct...if you say it works, great!

(5) The Send,%FilePath2% should be SendRaw,%FilePath2%, because these keys are valid in file names:

!    ^    +    #    {    }

You want AutoHotkey to interpret those keys literally in file names, not translate them. SendRaw is explained here:
https://www.autohotkey.com/docs/commands/Send.htm

Regards, Joe
Luis DiazIT consultant

Author

Commented:
Noted Joe,
Here is the revised version:

+F9::
;~ Put a file exist condition related to the path
FilePath1:=Clipboard
FilePath2:=StrReplace(FilePath1, """")
If (Not FileExist(FilePath2))
{
	Msgbox,0x1010,Error, Your clipboard which report the following value %FilePath2% doesn't exist	
	Return
}
Else
	Msgbox,	0x1040,Success, Your clipboard which report the following value %FilePath2% exist	
WinGetActiveTitle,Title
If (InStr(Title,"Google Drive"))
{
	PostMessage,0x112,0xF030,,,%Title%
    Sleep, 200
	Send, mc  	
	Sleep, 200
	Send, {Down 2}
	Sleep, 200
	Send, {Enter}
	Sleep, 1500
	WinGetActiveTitle,Title
	If (InStr(Title,"Open"))
	{	
		SendRaw, %FilePath2%
		Sleep, 200
		Send, {Enter}
		Return
	}
	Else
		MsgBox,0x1010,Error,Unable to send path as active window doesn't contains name expected
		Return
}
Else
  MsgBox,0x1010,Error,Google Drive is not the active window
Return

Open in new window


-I modified MsgBox code as recommended
-I indented line 10
-I modified Send to SendRaw for sending %FilePath2%
-I also increase a the sleep to ensure a proper sequence. Even if it works with the previous version I prefer to have a high sleep to avoid errors in the future

Let me know if you see some additional errors.

I also start to note in an excel file all the recommendations that you mentioned before in order to avoid making same mistakes in the future. I will try to remember them.

Thank you for your help.
Joe WinogradDeveloper
Fellow 2017
Most Valuable Expert 2018
Commented:
Hi Luis,
The updated code is much improved. Some comments on it:

Line 16: Has two extra spaces. It should be at the same indenting level as the PostMessage above it and the Send below it. Update: I just realized what the problem is. This line uses the space character at the beginning (four of them)...the lines above and below use the tab character (just one). I've never used tab characters in my code, but if you like it, I guess it's fine (I didn't even know that the AutoHotkey interpreter allowed tab characters in source code...obviously, it does).

Lines 26-29: You need to make a decision on how many spaces that you want to use for each indentation level and stick to it in all your code. I use two. Many people like four. Your lines above the If (InStr(Title,"Open")) statement use two. Your lines below it use eight. Update: I wrote this comment before realizing that the issue is the tab character. There really aren't eight spaces...there are two tab characters. How the code looks depends entirely on how the product in which you're viewing the code handles tab characters.

Line 33: The Return statement is at the wrong indentation level, and it's extremely important to understand why, namely, it is not executed in the Else path. If you want it executed in the Else path, the code should be:

Else
{
  MsgBox,0x1010,Error,Unable to send path as active window doesn't contains name expected
  Return
}

Open in new window

I hope you understand that...it is critically important in your AutoHotkey programming. As usual, the documentation on it is very good:
https://www.autohotkey.com/docs/commands/Else.htm

Line 36: This line has two space characters (no tabs) at the front. You need to clean all this up and get consistent with spaces or tabs at the beginning of lines.

Regards, Joe
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Luis DiazIT consultant

Author

Commented:
Noted Joe, unable to review it right now. I will keep you informed.
Luis DiazIT consultant

Author

Commented:
Hi Joe,
Prior to review your comment I just want to clarify a point.
Line 24 refers to and if condition which is inside an if condition (line 13). in AutoHotkey the indentation is required for this specific line or not?
For example in VBA I indent and if condition if it is inside another if condition. However I don't know what is the best practice in AutoHotkey.
Thank you in advance for your feedback.
Joe WinogradDeveloper
Fellow 2017
Most Valuable Expert 2018
Commented:
in AutoHotkey the indentation is required for this specific line or not?
No! In fact, indentation is never required in AutoHotkey. To be perfectly clear on this, indentation has no effect whatsoever on execution. It is there to help you follow the logic (flow of control) in your program. It is for readability/clarity.
I don't know what is the best practice in AutoHotkey.
The best practice in AutoHotkey and, I would argue, any programming language, is to indent so that it is clear what statements get executed in what conditional paths.

Let me give you an example. Here's an Else path in an AutoHotkey script that I published in an article and a video at EE (actually, there's a second video):

Else
{
  ; OutputFolder not set in config file - prompt for it
  Loop
  {
    FileSelectFolder,OutputFolder,,3,Browse to output folder or type/paste name into Folder box`nOr click the Make New Folder button to create a new folder
    If (ErrorLevel!=0)
    {
      MsgBox,4149,%ProgramName% Error,Output folder must be specified`n`nClick Retry to try again or Cancel to exit
      IfMsgBox,Cancel
        ExitApp
      Else
        Continue
    }
    If (SubStr(OutputFolder,0,1)!="\") ; see if OutputFolder has a backslash
      OutputFolder:=OutputFolder . "\" ; it doesn't - add it
    If (!FileExist(OutputFolder))
    {
      MsgBox,4149,%ProgramName% Error,Output folder %OutputFolder% does not exist`n`nClick Retry to try again or Cancel to exit
      IfMsgBox,Cancel
        ExitApp
      Else
        Continue
    }
    Break
  }
}

Open in new window

There are five levels of indentation in that code (with zero to eight spaces, i.e., a factor of two spaces for each indentation level). The idea is to make it clear which code gets executed under which conditions. Here's what the code looks like without indentation:

Else
{
; OutputFolder not set in config file - prompt for it
Loop
{
FileSelectFolder,OutputFolder,,3,Browse to output folder or type/paste name into Folder box`nOr click the Make New Folder button to create a new folder
If (ErrorLevel!=0)
{
MsgBox,4149,%ProgramName% Error,Output folder must be specified`n`nClick Retry to try again or Cancel to exit
IfMsgBox,Cancel
ExitApp
Else
Continue
}
If (SubStr(OutputFolder,0,1)!="\") ; see if OutputFolder has a backslash
OutputFolder:=OutputFolder . "\" ; it doesn't - add it
If (!FileExist(OutputFolder))
{
MsgBox,4149,%ProgramName% Error,Output folder %OutputFolder% does not exist`n`nClick Retry to try again or Cancel to exit
IfMsgBox,Cancel
ExitApp
Else
Continue
}
Break
}
}

Open in new window

When you read the code, can you see the difference in clarity that the indentation makes? I hope so. Regards, Joe

P.S. Going offline now for the rest of today. Will check back into the thread tomorrow.
Luis DiazIT consultant

Author

Commented:
Joe, thank you very much for your last comment.
I realized that there is an issue with the tab interpretation with EE so I decided to send you the proposal attached.
I take a decision to indent as proposed by sciTE4AutoHotkey through tab. So every time that I have a { I indent next line with tab.
Concerning Else condition from Line 31 to 35, Else is related to:
If (InStr(Title,"Open"))

Open in new window

If I don't get the active windows activated by the send keys in prior sequence I don't send the path.
I tested by putting an other title different that the one expected.
If (InStr(Title,"ttttttt"))

Open in new window

And I get the expected result:
20190922_221617-error-active-windows.pngLet me know what do you think.
29158834_send-clipboard-google-drive.ahk
Joe WinogradDeveloper
Fellow 2017
Most Valuable Expert 2018
Commented:
> So every time that I have a { I indent next line with tab.

Nice! All indented lines are consistent now with one or two tabs, except for line 16 (Sleep, 200)...that has no tabs and four spaces.

> Concerning Else condition from Line 31 to 35, Else is related to: If (InStr(Title,"Open"))

Correct...it is the False path for that If test.

> If I don't get the active windows activated by the send keys in prior sequence I don't send the path.

Correct...you're displaying an error MsgBox in that case...good programming practice.

> Let me know what do you think.

The indentation looks good, except for two cases where you have a single statement (not a block of statements), namely, lines 11 and 38 should both be indented because they are both in the Else path above it. If you're unclear on blocks, read the AutoHotkey doc on it here:

https://www.autohotkey.com/docs/commands/Block.htm

An easy way to visualize the indentation issue with single statements is to stop using them following an If or Else, i.e., always use braces to create a block. So, in these two cases, change them to:


Else
{
	Msgbox,	0x1040,Success, Your clipboard which report the following value %FilePath2% exist	
}

Open in new window

Else
{
	MsgBox,0x1010,Error,Google Drive is not the active window
}

Open in new window

That should clear up the indentation issue for you and make it obvious why those two MsgBox commands should be indented. The braces will also prevent future bugs when you meant to add statements to the Else path but forgot the braces...I can't tell you how many times I've seen that bug in AutoHotkey code! Regards, Joe
Luis DiazIT consultant

Author

Commented:
Joe,
Thank you very much for your message.
Just some comments that I would like to add:
All indented lines are consistent now with one or two tabs, except for line 16 (Sleep, 200)...that has no tabs and four spaces.
I am trying to understand this but I am not able to fully understand. As a result I made a video in which it shows that all the block after { has the same indentation. In fact in sciTE4AutoHotkey indentation is automatically generated when you put a { and you heat enter you can see this in my video.
Concerning the block recommendation I am fully agree, the best is to put a { after each if or else. By doing so you can properly manage indentation as it is generated automatically when you heat enter as it is shown in the video.
I attached video and last version of the AutoHotkey. I think that I am ready to add to my AutoHotkey file. Let me know.
Thank you for your help.
Video-send-clipboard-indentation_2019.7z
29158834_send-clipboard-google-drive.ahk
Developer
Fellow 2017
Most Valuable Expert 2018
Commented:
> I am trying to understand this but I am not able to fully understand.

My guess is that SciTE4AutoHotkey hides the issue and displays that line consistently with the block structure around it (it's being clever). As I've mentioned before, I never use SciTE4AutoHotkey, so I'm not sure about that. But if you load the AutoHotkey file that is in your #a42946443 post into Notepad, you'll see the problem...looks like this:

space tab issue
The problem is fixed in the file that was in your last post (#a42947336)...that one has the first Sleep, 200 line correct...one tab and no spaces at the beginning.

> you can properly manage indentation as it is generated automatically when you heat [hit] enter

Very nice feature!

> I think that I am ready to add to my AutoHotkey file. Let me know.

I agree! You're good to go! Regards, Joe
Luis DiazIT consultant

Author

Commented:
Noted Joe.

Thank you very much for your help!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial