Clipboard If Else Statement Using Variables w/o clearing clipboard first

Get help with using AutoHotkey and its commands and hotkeys
survargs

Clipboard If Else Statement Using Variables w/o clearing clipboard first

22 Jul 2016, 17:01

Thanks for your time in this problem I am having.
I have read the many other forum posts but what I am wanting to do is the following:
Read the contents of my clipboard already stored before this script is executed and based on the contents of the clipboard, use an if statement to then....(do anything really)


I tried using a single variable :=clipboard and then use that variable in the if else statement, but that did not work.
I then tried to assign that variable to another variable and that did not work either.

Any help would be greatly appreciated.
What I have figured out but do not want to use is a clear clipboard at the begging and copy contents again, I want to use what is in the clipboard already so that when this program is executed, it is just referencing what is already there.

Thanks,

-Alex

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

sleep, 300

pageN := clipboard
sleep, 300


sleep, 150
If (pageN == "apple")
		MsgBox You found a fruit.
		
                  Else If (pageN == "carrot")                  
                                   MsgBox You found a vegetable
		
              
	Else
		MsgBox, You found something else
Return

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

sleep, 300

pageN := clipboard
sleep, 300
var1 == %pageN%

sleep, 150
If (var1 == "apple")
		MsgBox You found a fruit.
		
                  Else If (var1 == "carrot")                  
                                   MsgBox You found a vegetable
		
              
	Else
		MsgBox, You found something else
Return
Wicked
Posts: 40
Joined: 05 Jun 2016, 20:34

Re: Clipboard If Else Statement Using Variables w/o clearing clipboard first

22 Jul 2016, 17:39

I don't see why you can't just use Clipboard itself:

Code: Select all

If InStr(Clipboard, "Apple")
	MsgBox, Apple
Else If InStr(Clipboard, "Carrot")
	MsgBox, Carrot
Else
	MsgBox, Something else
Return
The Instr(), just in-case the Clipboard isn't exactly "apple".
survargs

Re: Clipboard If Else Statement Using Variables w/o clearing clipboard first

23 Jul 2016, 16:14

Thanks Wicked, that words flawlessly!

One other quick question. I ultimately wanted to do something like a word bank search per if statement.
Is there any way to simplify this without having say 20 "or If InStr() statements per category(i.e. fruit, vegetable, etc.)?

This did not work

Code: Select all

If InStr(Clipboard, "Apple" | "Orange" | "Pear")
	MsgBox, Fruit
Else If InStr(Clipboard, "Carrot" | "Lettuce" | "Cabbage")
	MsgBox, Vegetable;
Else
	MsgBox, Something else
Return
After reading some other posts, this did work:

Code: Select all

If InStr(Clipboard, "Apple") or If InStr(Clipboard, "Orange") or If InStr(Clipboard, "Pear")
	MsgBox, Fruit
Else If InStr(Clipboard, "Carrot") or If InStr(Clipboard, "Lettuce") or If InStr(Clipboard, "Cabbage")
	MsgBox, Vegetable
Else
	MsgBox, Something else
Return
Helgef
Posts: 4464
Joined: 17 Jul 2016, 01:02
Contact:

Re: Clipboard If Else Statement Using Variables w/o clearing clipboard first

23 Jul 2016, 19:18

This works too,

Code: Select all

If RegExMatch(Clipboard, "i)Apple|Orange|Pear") ; Remove the: i) to make case-sensitive.
	MsgBox, Fruit
Else If RegExMatch(Clipboard, "i)Carrot|Lettuce|Cabbage|Pizza")
	MsgBox, Vegetable
Else
	MsgBox, Something else
Return
It's better if you want to append, eg, the fruit list, while the script is running.
survargs
Posts: 8
Joined: 23 Jul 2016, 16:21

Re: Clipboard If Else Statement Using Variables w/o clearing clipboard first

24 Jul 2016, 22:28

Thanks Helgef for the additional help and for that tip(which I have a question about).

The following is from ahk's resource library:
Writes text to the end of a file (first creating the file, if necessary).

FileAppend [, Text, Filename, Encoding]

Select
Parameters ¶

Text

The text to append to the file. This text may include linefeed characters (`n) to start new lines. In addition, a single long line can be broken up into several shorter ones by means of a continuation section.

If Text is blank, Filename will be created as an empty file (but if the file already exists, its modification time will be updated).

If Text is %ClipboardAll% or a variable that was previously assigned the value of ClipboardAll, Filename will be unconditionally overwritten with the entire contents of the clipboard (i.e. FileDelete is not necessary).
When you say
It's better if you want to append, eg, the fruit list, while the script is running.
Are you saying there has to be a global running script with these databases consisting of a fruit list and a vegetable list "ready so to speak"

I was unsure what you meant by using FileAppend because from what I saw in the AHK documents, isn't that more for adding to the database when something is found, or something along those lines. Forgive me but I did not completely understood what you meant by that quoted statement.

Before any code is requested or written, let me explain what I am trying to do:
I have an excel file that has several rows and for simplicity, let's choose the building of a pizza:
Well I want to interact with an interactive form to build my pizza and on the excel spreadsheet I have headers like

customer name | phone number| crust | vegetables | fruits | meats | beverage brand | beverage size
john doe | 214-555-5378 | thin | olives | none | pepperoni | sprite | 2L



I want to go line by column by column on the same row in excel selecting the cell, pressing ^c and then using that value to look up that cell in many of these If RegExMatch(Clipboard, "i)Apple|Orange|Pear") script executions, and it is most likely almost 1 for every column.
For example, the one for customer name, will look up the name in a seperate file full of "club discount members" , etc..

using WinActivate to go to our interactive pizza form
and WinActivate Excel Spreadsheet or !Esc to go back to our excel sheet where we left off, the script will then go 1 cell to the right or {right} and then do the same thing again.

In summary, will your method of using FileAppend and keeping these different lists while the script is running only applicable if you have a master script with all of these "RegExMatch(Clipboard, "i)Apple|Orange|Pear")" functions within it or could you just call then say using the Run C:/User/MyDocs/fruit_list_search.ahk for example.
Right now, I am thinking about only calling the script once per row,going down each column one excel cell at a time.... say a script for customer name to compare %clipboard% to a group of people in our database, and another for vegetables, another for fruits, etc...

Apologies for my uncertainty on the recommendation but any help is greatly appreciated.

Thanks,

-Alex
User avatar
Blackholyman
Posts: 1292
Joined: 29 Sep 2013, 22:57
Facebook: socialjsz
Google: +Jszapp
Location: Denmark
Contact:

Re: Clipboard If Else Statement Using Variables w/o clearing clipboard first

25 Jul 2016, 06:40

he only said "append" not fileAppend... def Append

all the values aka lists can be loaded dynamically from plain text files or other formats like Excel

plain text file can be read using build in commands like fileRead or a parsing loop and a few more

if you have your values in excel lists you can load those as well you just need to use a more advanced method same goes for some other formats

maybe if you try and tell us step by step how you compare the things now and the files ( full path if you wish for working code ) that all of the info is stored in.

so we know both where the data that needs to be compared comes from and also where the data it needs to be compared with is stored...

this way we can better help you use the best available method to do it to do the compare...

this snippet will return true or false if the value in the clipboard is in column "A" of the active excel sheet:

Code: Select all

f3::
if Excel_find(RegExReplace(clipboard, "\R"))
   MsgBox found
else
   MsgBox not found
return

Excel_find(string) {
   oExcel := ComObjActive("Excel.Application") 			                  ;create a handle to a new excel application
   if (oExcel.Range("A:A").Find(string))                               ; make the necessary col changes
      return true
   else 
      return false
}
Also check out:
Courses on AutoHotkey

My Autohotkey Blog
:dance:
survargs
Posts: 8
Joined: 23 Jul 2016, 16:21

Re: Clipboard If Else Statement Using Variables w/o clearing clipboard first

26 Jul 2016, 13:43

thanks BHM for your help as well.
Appreciate the promptness as well and apologize for my delayed response.
I had worked on this heavily and had to make a decision because after each answer I was getting, it was becoming more clear that I will probably have to go an alternate route to figure out what I would like to do.


What I have come up with is a series of concatenate formulas within excel to help me build my ahk scripts
for example:

for toppings like pepperoni, I have a formula that will check to see if that field has a 0 or anything greater.
If it has a 1(aka yes this pizza will have pepperoni in it), then I have the output to a script that I have written to do "X" on our order builder screen.

so what will be in the excel cell is an if statement or a concatenate at times but the output will always be something like this if copied:
C:\Duo\Toppings\1pepperoni.ahk
The above is obtained when the cell is copied even when using ^c and is a hyperlink equation "=hyperlink(B2)"to the cell right beside it

I have searched through this forum to see how one could run the link without clicking because I want to keep my mouse where it is at on the builder screen because that is where the script will run which uses the "CoordMode, Mouse, Screen" functionality within AHK

the cell I am copying in the following script is a cell (in our example, we have tabbed over and have the following cell in focus (cell A2) within excel that says =hyperlink(B2) that when copied contains C:\Duo\Toppings\2pepperoni.ahk when copied.

What I have been doing in the meantime is something like this:

Code: Select all

Run C:\A Steel Trap\Duo\best ahk\pizza_build\focus_spreadsheet.ahk   ;focus on excel spreadsheet to get next topping
send ^c  ;copy toppings(pepperoni) cell from excel which is really a built hyperlink
sleep, 400
Run C:\A Steel Trap\Duo\best ahk\pizza_build\focus_builder.ahk   ;this just uses WinActivate to focus on pizza builder
sleep, 400
send #r
sleep, 400
send ^v  ;enter script url in windows run command to execute program
sleep, 400
send {enter}  ;execute script

return

My entire excel spreadsheet has different headers and every row is an individual order so at the end of every one of these executions of ahk scripts, I have to use WinActivate to focus on the excel spreadsheet again and then tab over a certain number of times to then get to my next hyperlink field I will want to run.

It looks something like

Code: Select all

Run C:\A Steel Trap\Duo\best ahk\pizza_build\focus_excel.ahk
sleep, 1500
send {Tab 3}  ;tab over to next field we will need to use
sleep, 400
send ^c   ;copy 2nd AHK script to run
sleep, 400
Run C:\A Steel Trap\Duo\best ahk\pizza_build\focus_builder.ahk
sleep, 400
send #r
sleep, 400
send ^v  ;enter script url in windows run command to execute program
sleep, 400
send {enter}  ;execute script
return
Thanks for your help in advance and I went with the following excel commands because it was getting a little too long to write within the ahk programs:
=IF(COUNTIF
=INDEX in combination with MATCH
=INDEX
survargs
Posts: 8
Joined: 23 Jul 2016, 16:21

Re: Clipboard If Else Statement Using Variables w/o clearing clipboard first

29 Jul 2016, 02:14

Helgef wrote:This works too,

Code: Select all

If RegExMatch(Clipboard, "i)Apple|Orange|Pear") ; Remove the: i) to make case-sensitive.
	MsgBox, Fruit
Else If RegExMatch(Clipboard, "i)Carrot|Lettuce|Cabbage|Pizza")
	MsgBox, Vegetable
Else
	MsgBox, Something else
Return
It's better if you want to append, eg, the fruit list, while the script is running.

Hey Helgef,

Appreciate the help as I got through writing my code only to find out that this RegExMatch and In string search does not work for this particular example that i need it for.

Code: Select all

If InStr(Clipboard, "L")
	MsgBox, large
Else If InStr(Clipboard, "XL")
	MsgBox, x-large
	Else If InStr(Clipboard, "3XL")
	MsgBox, 3x-large
Else
	MsgBox, Something else
Return
and

Code: Select all

If RegExMatch(Clipboard, "i)L") ; Remove the: i) to make case-sensitive.
	MsgBox, large
Else If RegExMatch(Clipboard, "i)XL")
	MsgBox, extra large

Else If RegExMatch(Clipboard, "i)3XL")
	MsgBox, triple xl
Else
	MsgBox, Something else
Return
both the programs above yield results in the first occurence of the letter L if "3XL" is copied. Is there any way to allow me to copy say "3XL" in my clipboard and have it pop up with the message box "3XL?"
It had not occurred to me when I asked for help that I would be running into this issue and for that I apologize that you have to revisit this question of mine.

Appreciate the help!

-Alex
Helgef
Posts: 4464
Joined: 17 Jul 2016, 01:02
Contact:

Re: Clipboard If Else Statement Using Variables w/o clearing clipboard first

29 Jul 2016, 04:17

Hi, i think i might have some issues with my notification settings, I didn't see your follow-up questions. Anyway, you can try this:

Code: Select all

If RegExMatch(Clipboard, "i)^L$") ; Remove the: i) to make case-sensitive.
	MsgBox, large
Else If RegExMatch(Clipboard, "i)^XL$")
	MsgBox, extra large
Else If RegExMatch(Clipboard, "i)^3XL$")
	MsgBox, triple xl
Else If RegExMatch(Clipboard, "i)^4XL|5XL|6XL$")	; To demonstrate how to use ^ $ if more than one alternative.
	MsgBox, 3XL+
Else
	MsgBox, Something else
Return
survargs
Posts: 8
Joined: 23 Jul 2016, 16:21

Re: Clipboard If Else Statement Using Variables w/o clearing clipboard first

29 Jul 2016, 10:18

Helgef,

No problem, thank you thank you!

This was the last piece of the puzzle I needed to automate this process.

Please disregard any of the other questions I had as I ended up going with a copy and #r ^v (windows run and paste) method to execute ahk programs to which I generated hyperlinks in excel based on different criteria.

I will close this out. Thanks to everyone for your help!

-Alex

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Greast, mikeyww, Renegaduh, Xtra and 28 guests