Jesus... i totally forgot about it.
What a waste of topic space i am.
That v and g for goto in the gui made more than enough trouble for me a while back. I just got back to making some new ahk scripts and forgot about those nig nags. xD
So, since i posted the not working one, should at least post the working one.
I hated when i find a topic that has something that could help me but the examples are missing and instead someone just says, i figured it out. xD
This is the currently final version of the bookmark editor/manager backend script for a Rainmeter skin of mine.
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.
; theimmersion
; I will be comenting the code as best i can and only key points that i think should be mentioned.
; The rest should be explorable or self explanitory. :)
; You can always ask or suggest. :)
; Hope its useful for some people.
; Standard procedure.
#Persistent
#SingleInstance, Force
; This will be the global to set when something changes so we dont call uneeded functions.
; Especially true if there are read and writes of files involved and with loop.
; The files can get big and re-reading or re-writing them is pointless.
vDataChanged = 0
; Read the configuration file for how many entries there are in a file that will be used to modify.
; In this case, "In" stands for Item Number that is being used to render the amount of bookmarks by a different app.
; This information will be used to add appropriate amout of entries and to populate them with the data.
; Example: 2 Items = 2 sets of data in "Bookmars.txt". Every set contains two variables, the name and the address of the bookmark.
; So in the "Config.inc" it looks like this:
; [Variables]
; In=2 <- This will be used to set the count for all loops.
; The data that is being manipulated is in "Bookmarks.txt" and looks like this:
; [Variables]
; Bookmark1Name=Google
; Bookmark1Address=www.google.com
; Bookmark2Name=AHK
; Bookmark2Address=www.autohotkey.com
IniRead, vReadIn, Config.inc, Variables, In, Error reading variable!
Gui,+AlwaysOnTop
; Add a label.
Gui, Add, Text, h20, #
; Add X amount of numbers in a column direction.
Loop, %vReadIn%
{
Gui, Add, Text, y+10 h+16, %A_index%
}
; Add a label.
Gui, Add, Text, ys h20, BOOKMARK NAME
; Add X amount of Edit boxes and populate them with data from "Bookmarks.txt".
Loop, %vReadIn%
{
IniRead, vReadName, Bookmarks.txt, Variables, Bookmark%A_index%Name,
Gui , Add , Edit , w+130 h20 vStoreName%A_index%, %vReadName%
}
; Add a label.
Gui, Add, Text, ys h20, BOOKMARK ADDRESS
; Add X amount of Edit boxes and populate them with data from "Bookmarks.txt".
Loop, %vReadIn%
{
IniRead, vReadAddress, Bookmarks.txt, Variables, Bookmark%A_index%Address,
Gui , Add , Edit , w+400 h20 vStoreAddress%A_index%, %vReadAddress%
}
; Add a label.
Gui, Add, Text, ys h20, #
; Add X amount of Buttons named in numbered order to be used for switching bookmarks around.
Loop, %vReadIn%
{
Gui , Add , Button , w+20 h20 gSwitch, %A_index%
}
; Adds controls and info text to tell user what the script is doing. Pretty self explanitory.
Gui, Add, Button, xm+250 h20 gSave, Save ; Button save all the entries to "Bookmarks.txt"
Gui, Add, Button, x+1 h20 gGuiClose, Close ; Button to close/exit script.
Gui, Add, Button, x+1 h20 gRefresh, Refresh ; Button to re-read all the data again.
Gui, Add, Text, ys h20, SWAP ; Label
Gui, Add, Text, w+50 h20 +Wrap vShowSwapA, ; Show first selected item to be swaped in text form.
Gui, Add, Button, h20 gSwitchEntries, Swap ; Button to swap selected entries.
Gui, Add, Text, h20, ; White space, delimeter.
Gui, Add, Text, w+150 h20 vInfo, ; Info of what script is doing in text form. Ex: Saved, Not saved, Error etc...
Gui, Add, Text, xp+50 ys h20, ; White space, delimeter.
Gui, Add, Text, w+50 h20 +Wrap vShowSwapB, ; Show second selected item to be swaped in text form.
Gui, Add, Button, h20 gClearSwitch, Clear ; Button to clear the swap selections.
; Show gui, very important! xD
Gui , Show
Return
Switch:
If (vSwitchA = "") ; If nothing is yet selected, enable user to select essentially a row number for swaping.
{
MouseGetPos,,, id, NN
ControlGetText, vSwitchA, %NN%, ahk_id %id%
GuiControl,,ShowSwapA, Swap: %vSwitchA%
}
Else If (vSwitchB = "") ; Select the second row to be swaped with.
{
MouseGetPos,,, id, NN
ControlGetText, vSwitchB, %NN%, ahk_id %id%
GuiControl,,ShowSwapB, With: %vSwitchB%
}
Return
ClearSwitch: ; Clear the selection and information if user made a mis-click.
vSwitchA := ""
vSwitchB := ""
GuiControl,,ShowSwapA,
GuiControl,,ShowSwapB,
Return
SwitchEntries: ; Switch out the entires for each other by first placing all data in a temporary container.
Gui, Submit, NoHide
TempNameA := StoreName%vSwitchA%
TempAddressA := StoreAddress%vSwitchA%
TempNameB := StoreName%vSwitchB%
TempAddressB := StoreAddress%vSwitchB%
GuiControl,,StoreName%vSwitchA%,%TempNameB%
GuiControl,,StoreAddress%vSwitchA%,%TempAddressB%
GuiControl,,StoreName%vSwitchB%,%TempNameA%
GuiControl,,StoreAddress%vSwitchB%,%TempAddressA%
vSwitchA := ""
vSwitchB := ""
GuiControl,,ShowSwap,
vDataChanged = 1 ; Now we changed something so we know we can enable saving.
Return
; We modified the "Bookmarks.txt" externally with a text editor or we swaped entries but dont want to commit-
; and want to re-read for new edits without exiting or reloading script.
Refresh:
Loop, %vReadIn%
{
IniRead, vReadName, Bookmarks.txt, Variables, Bookmark%A_index%Name,
GuiControl,,StoreName%A_index%,%vReadName%
IniRead, vReadAddress, Bookmarks.txt, Variables, Bookmark%A_index%Address,
GuiControl,,StoreAddress%A_index%,%vReadAddress%
}
vDataChanged = 0 ; Now we know everything is back to original state with no changes so we disable saving.
; Tell the user it has been refreshed for less confusion if user didnt change anything but wasnt sure and refreshed anyway-
; so he knows the command worked.
GuiControl,,Info,Refreshed
; Reload
Return
Save: ; Save all the data.
If (vDataChanged = 1) ; Check if anything is modifed first before commiting to wirting to file.
{
Gui, Submit, NoHide
Loop, %vReadIn%
{
vNameData := StoreName%A_index%
IniWrite, %vNameData%, Bookmarks.txt, Variables, Bookmark%A_index%Name
IniRead, vReadName, Bookmarks.txt, Variables, Bookmark%A_index%Name,
GuiControl,,StoreName%A_index%,%vReadName%
}
Loop, %vReadIn%
{
vAddressData := StoreAddress%A_index%
IniWrite, %vAddressData%, Bookmarks.txt, Variables, Bookmark%A_index%Address
IniRead, vReadAddress, Bookmarks.txt, Variables, Bookmark%A_index%Address,
GuiControl,,StoreAddress%A_index%,%vReadAddress%
}
; This is for the external app that is using data from "Bookmarks.txt" to render them on screen.
; This app sets the item count variable "In" that it renders in "Config.inc" and the variable sets for names and addresses in "Bookmarks.txt".
; Like we dont know what Rainmeter is or does. xD But can be used by any program i guess?
; After changes are made and saved to "Bookmarks.txt", this command will refresh the app to show the changes on screen.
; Remove if not used for that program.
SendMessage, 0x111, 4001,,, ahk_class RainmeterMeterWindow
GuiControl,,Info,Saved ; Tell the user that we saved new data.
vDataChanged = 0 ; We saved the changes so we revert back the state to not modified so user cant save twice, second being the same data.
}
Else
GuiControl,,Info,Not saved, nothing edited. ; Tell user that he dint change anything so no need to write same data to file again.
; Goto,GuiClose ; Optional, we can either close the program after each save or we can leave it open for user to inspect one last time.
Return
; Close app. Also very important.
GuiClose:
ExitApp
MsgBox, Error closing script!
Return
; Lots of white space because i like to do stuff around the middle of my screen but if no white space here, cant scroll enough. xD
; Im weird...
I tried to comment it as best as i can. Dont do much tutorials, hope this helps someone out. I found so many good help in random posts that had exactly what i needed.
Sometimes even had something i never knew i needed. xD