AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Commands as functions
Goto page Previous  1, 2, 3
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Titan



Joined: 11 Aug 2004
Posts: 5043
Location: /b/

PostPosted: Tue Jan 01, 2008 6:45 pm    Post subject: Reply with quote

Thanks it's been fixed.
_________________
Chat (IRC)PlusNetScriptsIronAHK Contact by email not private message.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Guest






PostPosted: Tue Jan 01, 2008 9:19 pm    Post subject: Reply with quote

bmcclure wrote:
Can't the first parameter be removed since it doesn't matter what was in the variable before and it's returned by the function?
Titan wrote:
Thanks it's been fixed.

...I'm surprised Titan...no complaints, just "ok!"...if you are going for 100% compatibility (same syntax for command & function), I think it should be...

Code:
;//Command Syntax: IniRead, OutputVar, Filename, Section, Key[, Default]
IniRead(ByRef OutputVar, Filename, Section, Key, Default = "") {
   IniRead, OutputVar, %Filename%, %Section%, %Key%, %Default%
   return, OutputVar
}

...I would expect you to think that command & function syntax should match...

Actually that might complain too much about the ByRef, so maybe this would be better...

Code:
;//Command Syntax: IniRead, OutputVar, Filename, Section, Key[, Default]
IniRead(p_OutputVar, Filename, Section, Key, Default = "") {
   IniRead, OutputVar, %Filename%, %Section%, %Key%, %Default%
   %p_OutputVar%:=OutputVar
   return, OutputVar
}

...then you could call it with an OutputVar name or just "" for blank...I don't think ByRef's can be the emtpy string, so that workaround would allow it...testing code...

Code:
ret:=IniRead(OutputVar, Filename, Section, Key, Default)
ret:=IniRead("", Filename, Section, Key, Default)
ret:=IniRead("OutputVar", "123.ini", "Section", "Key", "Default")
ret:=IniRead("", "123.ini", "Section", "Key", "Default")
IniRead(OutputVar, "123.ini", "Section", "Key", "Default")

...ret & OutputVar should match if both are specified, otherwise return value is in ret only or OutputVar only...
Back to top
Titan



Joined: 11 Aug 2004
Posts: 5043
Location: /b/

PostPosted: Tue Jan 01, 2008 9:35 pm    Post subject: Reply with quote

JSLover wrote:
I would expect you to think that command & function syntax should match
No I don't. My intentions with this library since the beginning was to make a function of every command that had an OutputVar which would return its value rather than saving it to a variable. This provides greater flexibility with use in expressions, and makes your code that little bit shorter. Multiple output commands are obviously an exception and I only implemented them on a whim since they break consistency.
_________________
Chat (IRC)PlusNetScriptsIronAHK Contact by email not private message.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
bmcclure



Joined: 24 Nov 2007
Posts: 766

PostPosted: Tue Jan 01, 2008 9:51 pm    Post subject: Reply with quote

I agree with Titan also. The great part about these functions is that you can chain them together in many cases, since they return the result directly.

If you used a ByRef, or asked for the OutputVar as a parameter, you would have to keep it to one IniRead (or whatever command) per line.

It would just be redundant to keep the 'v' parameter in there, since it's not and doesn't need to be a ByRef, and the function doesn't care what the value of the output var is since it gets overwritten anyway.

And I do completely agree with the ByRefs on the string commands since it avoids re-reading the whole string as it's passed to each function, especially helpful for really long strings.

I'm happy with the changes Smile
_________________
Ben

My Trac projects
My Wiki
[Broken] - My music
Back to top
View user's profile Send private message
Krogdor



Joined: 18 Apr 2008
Posts: 1381
Location: The Interwebs

PostPosted: Mon May 26, 2008 8:32 am    Post subject: Reply with quote

Having a problem with Run()...

The Error Message wrote:
Error in #include file "C:\[censored]\Lib\CommandFunctions.ahk": This dynamic variable is blank. If this variable was not intended to be dynamic, remove the % symbols from it.

Specifically: %v%

Line#
161: Return,v
162: }
163: {
164: RegRead,v,%RootKey%,%SubKey%,%ValueName%
165: Return,v
166: }
167: {
---> 168: Run,%Target%,%WorkingDir%,%Mode%,%v%
169: Return,v
170: }
171: {
172: SoundGet,v,%ComponentType%,%ControlType%,%DeviceNumber%
173: Return,v
174: }
175: {

---------------------------
OK
---------------------------


...Errr. Don't really know what to do with this? x_x

Edit: Nvm, found the problem... thought it was referring to %target%, just noticed it was actually %v%, removed the %'s and it worked fine. So, if anyone else has this problem.. just remove the %'s... yea..
Back to top
View user's profile Send private message AIM Address
heresy



Joined: 11 Mar 2008
Posts: 291

PostPosted: Mon Jun 23, 2008 8:46 pm    Post subject: Reply with quote

i thought it was in the library already

Code:
If ProcExist("Notepad.exe")
    WinClose,% "ahk_pid " Errorlevel

Code:
ProcExist(PID_or_Name=""){
    Process, Exist, % (PID_or_Name="") ? DllCall("GetCurrentProcessID") : PID_or_Name
    Return Errorlevel
}

_________________
Easy WinAPI - Dive into Windows API World
Benchmark your AutoHotkey skills at PlayAHK.com
Back to top
View user's profile Send private message
Andrej Adamenko
Guest





PostPosted: Thu Sep 10, 2009 8:45 am    Post subject: GetKeyState() function in Titan's Functions.ahk Reply with quote

There is a problem with GetKeyState() function. It returns "U" or "D" as GetKeyState command does. But there is a built-in function with the same name which returns true or false. It is confusing and the following code may not work properly depending on Functions.ahk being or not being installed:

Code:
if GetKeyState("Shift")
{
   ;some code


I suggest removing GetKeyState() from the library.
Back to top
Andrej



Joined: 23 Sep 2009
Posts: 1

PostPosted: Thu Oct 29, 2009 1:47 pm    Post subject: a patch Reply with quote

Titan, thank you for the useful library. I found few bugs and fixed them and added few missing functions.

Andrej

Code:

Index: Functions.ahk
===================================================================
--- Functions.ahk   (revision 56)
+++ Functions.ahk   (working copy)
@@ -67,6 +67,16 @@
    EnvGet, v, %EnvVarName%
    Return, v
 }
+EnvAdd(Var, Value, TimeUnits) {
+   v:= Var
+   EnvAdd, v, %Value%, %TimeUnits%
+   Return, v
+}   
+EnvSub(Var, Value, TimeUnits) {
+   v:= Var
+   EnvSub, v, %Value%, %TimeUnits%
+   Return, v
+}   
 FileGetAttrib(Filename = "") {
    FileGetAttrib, v, %Filename%
    Return, v
@@ -148,7 +158,7 @@
    Return, v
 }
 Run(Target, WorkingDir = "", Mode = "") {
-   Run, %Target%, %WorkingDir%, %Mode%, %v%
+   Run, %Target%, %WorkingDir%, %Mode%, v
    Return, v   
 }
 SoundGet(ComponentType = "", ControlType = "", DeviceNumber = "") {
@@ -167,43 +177,44 @@
    SplitPath, InputVar, OutFileName, OutDir, OutExtension, OutNameNoExt, OutDrive
 }
 StringGetPos(ByRef InputVar, SearchText, Mode = "", Offset = "") {
-   StringGetPos, v, %InputVar%, %SearchText%, %Mode%, %Offset%
+   StringGetPos, v, InputVar, %SearchText%, %Mode%, %Offset%
    Return, v
 }
 StringLeft(ByRef InputVar, Count) {
-   StringLeft, v, %InputVar%, %Count%
+   StringLeft, v, InputVar, %Count%
    Return, v
 }
 StringLen(ByRef InputVar) {
-   StringLen, v, %InputVar%
+   StringLen, v, InputVar
    Return, v
 }
 StringLower(ByRef InputVar, T = "") {
-   StringLower, v, %InputVar%, %T%
+   StringLower, v, InputVar, %T%
    Return, v
 }
 StringMid(ByRef InputVar, StartChar, Count , L = "") {
-   StringMid, v, %InputVar%, %StartChar%, %Count%, %L%
+   StringMid, v, InputVar, %StartChar%, %Count%, %L%
    Return, v
 }
-StringReplace(ByRef InputVar, SearchText, ReplaceText = "", All = "") {
-   StringReplace, v, %InputVar%, %SearchText%, %ReplaceText%, %All%
+StringReplace(inputStr, SearchText, ReplaceText = "", All = "") {
+   InputVar:= inputStr
+   StringReplace, v, InputVar, %SearchText%, %ReplaceText%, %All%
    Return, v
 }
 StringRight(ByRef InputVar, Count) {
-   StringRight, v, %InputVar%, %Count%
+   StringRight, v, InputVar, %Count%
    Return, v
 }
 StringTrimLeft(ByRef InputVar, Count) {
-   StringTrimLeft, v, %InputVar%, %Count%
+   StringTrimLeft, v, InputVar, %Count%
    Return, v
 }
 StringTrimRight(ByRef InputVar, Count) {
-   StringTrimRight, v, %InputVar%, %Count%
+   StringTrimRight, v, InputVar, %Count%
    Return, v
 }
 StringUpper(ByRef InputVar, T = "") {
-   StringUpper, v, %InputVar%, %T%
+   StringUpper, v, InputVar, %T%
    Return, v
 }
 SysGet(Subcommand, Param3 = "") {

Back to top
View user's profile Send private message
Tuncay



Joined: 07 Nov 2006
Posts: 897
Location: Berlin, DE

PostPosted: Thu Feb 25, 2010 12:53 pm    Post subject: Reply with quote

Is it somehow me orwhy do that not work?
Code:
; #Include Functions.ahk
#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%

; Call this once, if the library is not explicitly included.
Functions()
MsgBox % StringReplace(var := "hello world!", "!", "?", True)


The source for this function is
Code:
StringReplace(ByRef InputVar, SearchText, ReplaceText = "", All = "") {
   StringReplace, v, %InputVar%, %SearchText%, %ReplaceText%, %All%
   Return, v
}

Is it really correct? Are all functions tested? Why is InputVar made dynamic??? I think you made a mistake by all those commands where the inputvar is the name of the variable.
_________________
Download Ahk Standard Library Collection - An archive with stdlib compatible function libraries
Back to top
View user's profile Send private message
Titan



Joined: 11 Aug 2004
Posts: 5043
Location: /b/

PostPosted: Thu Feb 25, 2010 2:53 pm    Post subject: Reply with quote

Fixed, thank you both Andrej and Tuncay.
_________________
Chat (IRC)PlusNetScriptsIronAHK Contact by email not private message.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
wolf.b



Joined: 18 Oct 2007
Posts: 4

PostPosted: Fri Feb 26, 2010 12:29 am    Post subject: Reply with quote

Topic 1: Run() is not working

It has been reported twice before by Krogdor in May 2008, and also by Andrej in Oct 2009. Maybe it got fixed in a previous version, but now it is broken again.
Please try this demonstration code:
Code:
If Not Functions() ; make sure the stdlib gets loaded
    ExitApp
Run, notepad.exe,,, PID ; this is working
Gosub, ShowPID
PID := Run("notepad.exe") ; this is _not_ working
Gosub, ShowPID   
ExitApp

ShowPID:
    WinWaitActive, Untitled ahk_class Notepad
    WinClose, Untitled ahk_class Notepad
    MsgBox, Process ID was:`n%PID%
Return

It will work with the following change: (replace %v% in the second line with v)
Code:
Run(Target, WorkingDir = "", Mode = "") {
   Run, %Target%, %WorkingDir%, %Mode%, %v%
   Return, v   
}

Code:
Run(Target, WorkingDir = "", Mode = "") {
   Run, %Target%, %WorkingDir%, %Mode%, v
   Return, v   
}


Topic 2: Please consider renaming the "Boolean" functions
This is not really a request, but I would like to propose the following changes:
  • rename IfBetween() to Between()
  • rename IfContains() to Contains()
  • rename IfIs() to IsType()
  • remove IfIn() and all negative "Boolean" functions
I believe this would make code look and feel more natural (personal opinion). Also removing redundant functions would steamline the library. E.g. instead of writing this:
Code:
If IfBetween(var, 1, 5)
If IfNotContains(var, MatchList)
If IfIs(var, "integer")
If IfNotIn(var, MatchList)

One could write this:
Code:
If Between(var, 1, 5)
If Not Contains(var, MatchList)
If IsType(var, "integer")
If Not Contains(var, MatchList)

Just a thought, what do you think?


Wolf
_________________
Schön wär's, wenn's schön wär!
Back to top
View user's profile Send private message
Tuncay



Joined: 07 Nov 2006
Posts: 897
Location: Berlin, DE

PostPosted: Fri Feb 26, 2010 9:38 am    Post subject: Reply with quote

I agree with previous poster. What about compatibility then? And may be a complete review for functionality is needed.
_________________
Download Ahk Standard Library Collection - An archive with stdlib compatible function libraries
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page Previous  1, 2, 3
Page 3 of 3

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group