Link to home
Start Free TrialLog in
Avatar of Luis Diaz
Luis DiazFlag for Colombia

asked on

AutoHotkey: Send clipboard & upload file in google drive

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.
SOLUTION
Avatar of Joe Winograd
Joe Winograd
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Luis Diaz

ASKER

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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Noted Joe, unable to review it right now. I will keep you informed.
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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:
User generated imageLet me know what do you think.
29158834_send-clipboard-google-drive.ahk
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Noted Joe.

Thank you very much for your help!