GeekDude's Tips, Tricks, and Standalones

Put simple Tips and Tricks that are not entire Tutorials in this forum
GeekDude
Posts: 836
Joined: 02 Oct 2013, 22:13

Re: GeekDude's tips and tricks

23 Oct 2017, 11:48

For the first one, Dummy is static (a type of local where the value isn't erased when the function ends) to the MyFunction function, so you can only access it from inside MyFunction's body. From my tests, the behavior of static which is being used here for auto-run doesn't apply to global, so you'd need to trick it a bit if you want to set a global value in your function.

Code: [Select all] [Download] GeSHi © Codebox Plus

MyFunction()
{
global MyGlobal ; Makes MyGlobal global
static Dummy := MyGlobal := MyFunction() ; Makes Dummy static, and sets both it and MyGlobal to the result of MyFunction
MsgBox, I see you %A_UserName%
wow := "oops " A_Now
return wow
}

MsgBox, % MyGlobal





For the second one, you need to use the new Q/Quotes option on the continuation section, to disable automatic quote escaping.

Quotes (or Q): Restores the ability to terminate quoted strings when an expression is used in a continuation section.


Right now, it's treating your expression as if you put this:

Code: [Select all] [Download] GeSHi © Codebox Plus

MyObject := {math: `"fun`",english: `"boring`",coding: `"exciting`"}


You'll need to do this to make it work:

Code: [Select all] [Download] GeSHi © Codebox Plus

MyObject := 
( LTrim Quotes Join
{
math: "fun",
english: "boring",
coding: "exciting"
}
)
User avatar
derz00
Posts: 495
Joined: 02 Feb 2016, 17:54
GitHub: derz00
Location: Middle of the round cube

Re: GeekDude's tips and tricks

23 Oct 2017, 12:57

Sure enough that new option is mentioned on this page: https://autohotkey.com/v2/v2-changes.htm

Thank you for your very helpful (as usual) explanations. You've got a good handle on the language. I didn't know you even do much with v2?!
try it and see
...
User avatar
derz00
Posts: 495
Joined: 02 Feb 2016, 17:54
GitHub: derz00
Location: Middle of the round cube

Re: GeekDude's tips and tricks

23 Oct 2017, 14:12

Uhhh yes RUNIE? :P
try it and see
...
User avatar
RUNIE
Posts: 279
Joined: 03 May 2014, 14:50
GitHub: Run1e

Re: GeekDude's tips and tricks

23 Oct 2017, 14:46

I answered your question but didn't notice there was a page 2 haha smh
HotKeyIt
Posts: 1671
Joined: 29 Sep 2013, 18:35
Contact:

Re: GeekDude's tips and tricks

23 Oct 2017, 15:13

1. because it is a static and not a global variable:

Code: [Select all] [Download] GeSHi © Codebox Plus

MyFunction()
{
Static Dummy := MyFunction()
MsgBox, %Dummy% I see you %A_UserName%
wow:="oops " A_Now
return wow
}
MsgBox % MyFunction()


2.

Code: [Select all] [Download] GeSHi © Codebox Plus

MyObject := {math: "fun"
,english: "boring"
,coding: "exciting"}
User avatar
derz00
Posts: 495
Joined: 02 Feb 2016, 17:54
GitHub: derz00
Location: Middle of the round cube

Re: GeekDude's Tips, Tricks, and Standalones

08 Jun 2018, 19:01

GeekDude wrote:Find, and optionally replace, all matches of regular expression efficiently using a custom enumerator.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

Thanks for sharing. I like your object-oriented coding styles :). This is very nicely done, as always
try it and see
...
GeekDude
Posts: 836
Joined: 02 Oct 2013, 22:13

Re: GeekDude's Tips, Tricks, and Standalones

08 Jul 2018, 11:03

This post has been updated! Two new sections, Networking and Web and Files and Folders!

The DllCall section has been updated to include a new tip to "Run command line tools without having a command prompt pop."

You can see the exact changes from the revision history at the bottom of the original post.
User avatar
SpeedMaster
Posts: 204
Joined: 12 Nov 2016, 16:09

Re: GeekDude's Tips, Tricks, and Standalones

09 Jul 2018, 06:53

GeekDude wrote:Download a file from the web and use its contents without having to save to a temporary file.

It works fine as long as you stay connected :thumbup: but when you are not more connected, the script displays a really ugly warning. :sick:

I found a solution here : https://autohotkey.com/board/topic/1110 ... -internet/

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


Maybe there is a request that checks connection ? :think:


there is also a ready to use function on this page UrlDownloadToVar() :D

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

GeekDude
Posts: 836
Joined: 02 Oct 2013, 22:13

Re: GeekDude's Tips, Tricks, and Standalones

09 Jul 2018, 08:40

The recommended way of handling that issue is to put the code which triggers the error into a try/catch block. This allows you to run an alternate set of code in the case that the first set of code fails. Checking if you have an Internet connection before trying to make a request can work but it is probably not a good way to make your code more robust. Instead of handling a single point of failure in the code it creates two points of failure: one for the Internet check and one for the web request.

By instead using try/catch you can handle the case that there's no net connection, or the case that the particular website was unreachable, or the case that the server didn't respond/timed out, and many other cases all in one go. It's less code and easier to read/write :) .

Finally, that URLDownloadToVar function you shared suppresses COM related errors without ever re-enabling them or doing anything to handle them. This can break other code in a script that has been written to handle errors correctly. I have never seen a case where calling ComObjError(false) has been beneficial rather than subtly detrimental. The way to handle errors is not to disable the errors, but like I said above to use try/catch blocks to specify what should happen (instead of a warning dialog).

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



Thanks for your input on this topic! :)
User avatar
SpeedMaster
Posts: 204
Joined: 12 Nov 2016, 16:09

Re: GeekDude's Tips, Tricks, and Standalones

09 Jul 2018, 13:50

GeekDude wrote:By using try/catch you can handle the case that there's no net connection, or the case that the particular website was unreachable...

Thanks for your explanation.
I didn't know about try-catch statement. :roll:
This works very well. :thumbup:

Cheers
User avatar
SpeedMaster
Posts: 204
Joined: 12 Nov 2016, 16:09

Re: GeekDude's Tips, Tricks, and Standalones

11 Jul 2018, 14:09

GeekDude wrote:GUIs : Create a menu bar for your GUI using a well formatted object instead of a long list of menu commands.

Help wrote: Check, MenuItemName: Adds a visible checkmark in the menu next to MenuItemName (if there isn't one already).

Hello,
How do you "check/Uncheck" an item in the custom menu bar ? :think:

for example:

Code: [Select all] [Download] GeSHi © Codebox Plus

Menu MyMenu, Check, &Find


Thanks,
GeekDude
Posts: 836
Joined: 02 Oct 2013, 22:13

Re: GeekDude's Tips, Tricks, and Standalones

13 Jul 2018, 09:57

SpeedMaster wrote:How do you "check/Uncheck" an item in the custom menu bar ? :think:

When the function builds your menu out of your array structure, it creates an array and places the name of each menu into it. The indices of the array are in linear order according to where your sub-menus are placed into the array.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



So, then, to add a check to Menu -> Branch 1 -> Item 2 -> Sub-Item 2 you would call Menu, % MenuArray[3], Check, Sub-Item 2. Similarly, for Menu -> Branch 2 -> Item 1 you would call Menu, % MenuArray[5], Check, Item 1.

It's not the most user friendly of methods but it works. I suppose it could be possible to modify the menu creation function to return a more usable array (so that you could say MenuArray["Branch 1>Item 3"] instead of MenuArray[3] but the separator would need to be well chosen to avoid confusion if the menu name has that character in it. Alternately, it could be something like MenuArray["1:2"] for Branch 1 -> Item 3 but, while it does avoid shifting the index of later top-level menus when a sub-menu is added, it's not very useful for when you reorder the menu/add or remove top level menu items.
User avatar
SpeedMaster
Posts: 204
Joined: 12 Nov 2016, 16:09

Re: GeekDude's Tips, Tricks, and Standalones

13 Jul 2018, 19:33

Tanks !
It works now ;)

I made a simple mistake of syntax

Menu, % A_ThisMenu, Check, %A_ThisMenuItem%
Menu, % Menu_2, UnCheck, &Item2
Menu, Menu_2, UnCheck, &Item2 ; ---> The fix


Cheers
User avatar
jeeswg
Posts: 4811
Joined: 19 Dec 2016, 01:58
Location: UK

Re: GeekDude's Tips, Tricks, and Standalones

28 Jul 2018, 17:18

- Thanks so much for this tutorial, especially the 'Initialize()' and 'Download a file' examples.
- Btw notice the difference with StrRepeat, when using spaces or zeros as the fill character and specifying 0 characters. I also provide my StrRept function as an example, where I added negative number handling.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

- Should this be "Ptr" instead of "UInt"?
pData := NumGet(ComObjValue(Request.responseBody)+8+A_PtrSize, "UInt")

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 1 guest