RagsRevenge Posted February 17, 2010 Share Posted February 17, 2010 Hi, I may be suffering a bit of brain freeze, but I can't figure out the most elegant way of doing this. I want to call a couple of functions, which read/write an objects' properties. The majority of each function will be the exact same, but the object property will be the only difference. Core code: func _setKeywords($sFile, $sKeywords) $objFile.Open($sFile); bind to the summary information metadata attached to the file. $objFile.SummaryProperties.Keywords = $sKeywords $objFile.Save; save changes to file properties $objFile.Close EndFunc I may want to read/write other summaryProperties, like "Title", "Author", etc. Is there a way to pass the $objFile.SummaryProperties.Keywords or $objFile.SummaryProperties.Title to the function so that I can use the same function for all SummaryProperties? Thanks in advance, D Link to comment Share on other sites More sharing options...
PsaltyDS Posted February 17, 2010 Share Posted February 17, 2010 (edited) Execute(), as in: Global $oDictionary, $oMyError Global $aDictItems[3][2] = [["one","uno"], ["two","dos"], ["three","tres"]] ; Initialize a COM error handler $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc") ; Create dictionary object $oDictionary = ObjCreate("Scripting.Dictionary") ; Add key/item pairs For $n = 0 to UBound($aDictItems) - 1 ; Normal syntax: $oDictionary.ADD($aDictItems[$n][0], $aDictItems[$n][1]) $sMethod = "Add" Execute("$oDictionary." & $sMethod & "($aDictItems[$n][0], $aDictItems[$n][1])") Next ; Read items For $n = 0 to UBound($aDictItems) - 1 ; Normal syntax: $oDictionary.Item($aDictItems[$n][0]) $sMethod = "Item" $sItem = Execute("$oDictionary." & $sMethod & "($aDictItems[$n][0])") ConsoleWrite($aDictItems[$n][0] & " = " & $sItem & @LF) Next ; This is my custom defined error handler Func MyErrFunc() Local $err = $oMyError.number MsgBox(16, "Error", "COM Error: " & $err) SetError($err) ; to check for after this function returns EndFunc ;==>MyErrFunc Edit: Forgot COM error handler. Edited February 17, 2010 by PsaltyDS ss26 1 Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 17, 2010 Moderators Share Posted February 17, 2010 PsaltyDS,BZ. Another little nugget to store away. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
RagsRevenge Posted February 18, 2010 Author Share Posted February 18, 2010 Thanks PDS, the reading of properties works a charm, and as Melba says, it's a nice nugget to store away. I've been trying for the past hour to get writing of properties working, but I'm a little bamboozled. ;property that I want to write to: $objFile.SummaryProperties.Keywords $sPropertyToEdit = "Keywords" $sPropertyToAdd = "Test" Assign("objFile.SummaryProperties." & $sPropertyToEdit, $sPropertyToAdd) & @CRLF) ; Eval("objFile.SummaryProperties." & $sPropertyToEdit) returns "Test" Any assistance greatly appreciated, And thanks again for the Execute() tip. D Link to comment Share on other sites More sharing options...
PsaltyDS Posted February 18, 2010 Share Posted February 18, 2010 How about this? ;property that I want to write to: $objFile.SummaryProperties.Keywords $sPropertyToEdit = "Keywords" $sPropertyValue = "Test" ; Set property Execute("$objFile.SummaryProperties." & $sPropertyToEdit & " = " $sPropertyValue) ; Read-after-write verify $sVal = Execute("$objFile.SummaryProperties." & $sPropertyToEdit) ConsoleWrite("Debug: $sVal = " & $sVal & @LF) ; should return "Test" Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law Link to comment Share on other sites More sharing options...
RagsRevenge Posted February 22, 2010 Author Share Posted February 22, 2010 (edited) How about this? ;property that I want to write to: $objFile.SummaryProperties.Keywords $sPropertyToEdit = "Keywords" $sPropertyValue = "Test" ; Set property Execute("$objFile.SummaryProperties." & $sPropertyToEdit & " = " $sPropertyValue) ; Read-after-write verify $sVal = Execute("$objFile.SummaryProperties." & $sPropertyToEdit) ConsoleWrite("Debug: $sVal = " & $sVal & @LF) ; should return "Test" Hi PsaltyDS, and thanks for your ongoing assistance. I'm still having problems assigning to a property/variable using Execute(). I've broken it down into the simplest code expandcollapse popupGlobal $sum = 0 ;doesn't work modify("$sum") func modify($var) Execute($var & " = 5") MsgBox(0,"", $sum) ;0 is displayed EndFunc ;End doesn't work ;works modifyWorking("sum") func modifyWorking($var) Assign($var, "5") MsgBox(0,"", $sum) ;5 is displayed EndFunc ;end works $a=1 $v=Execute("$a=2") MsgBox(0,"", $v) ;False is displayed because 1 <> 2, so the execute is doing a comparison rather than an assignment. #cs ;Example taken from code I am trying to achieve. $sPropertyToEdit = "Keywords" $sPropertyToAdd = "Test" $propToExecute = "$objFile.SummaryProperties." & $sPropertyToEdit Local $bool = Execute($propToExecute & " = " & $sPropertyToAdd) msgbox(0,"", @error) ;@error shows 1, $bool is blank #ce #cs Local $bool = Assign("objFile.SummaryProperties." & $sPropertyToEdit, $sPropertyToAdd) ;note missing dollar sign for objFile as per example in help msgbox(0,"", $bool) ;shows 1, but value hasn't been updated. #ce #cs Local $bool = Assign("$objFile.SummaryProperties." & $sPropertyToEdit, $sPropertyToAdd) msgbox(0,"", $bool) ;shows 1, but value hasn't been updated. #ce I would be delighted to get this working, but I don't have any experience with execute/eval/assign. Thanks, D Edited February 22, 2010 by RagsRevenge SchneiMi 1 Link to comment Share on other sites More sharing options...
martin Posted February 22, 2010 Share Posted February 22, 2010 Global $sum = 0 modify("$sum") func modify($var) Local $s= Execute($var) MsgBox(0,"", $s + 5) ;5 is displayed EndFunc ;works modifyWorking("sum") func modifyWorking($var) Assign($var, "5") MsgBox(0,"", $sum) ;5 is displayed EndFunc ;end works $a=1 $v = 9 Assign("v",2) ;$v=Eval("$a=2") MsgBox(0,"", $v) Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script. Link to comment Share on other sites More sharing options...
RagsRevenge Posted February 23, 2010 Author Share Posted February 23, 2010 $v = 9 Assign("v",2) MsgBox(0,"", $v) Thanks Martin, so to assign "New Keyword" to $objFile.SummaryProperties.Keywords, I need to call Assign("objFile.SummaryProperties.Keywords", "New Keyword") Unfortunately, this isn't working for me. Any tips greatly appreciated. D Link to comment Share on other sites More sharing options...
PsaltyDS Posted February 23, 2010 Share Posted February 23, 2010 Did you try: Execute("$objFile.SummaryProperties.Keywords = 'New Keyword'") Or: $sValue = 'New Keyword' Execute("$objFile.SummaryProperties.Keywords = '" & $sValue & "'") Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law Link to comment Share on other sites More sharing options...
RagsRevenge Posted February 24, 2010 Author Share Posted February 24, 2010 Did you try: Execute("$objFile.SummaryProperties.Keywords = 'New Keyword'") Or: $sValue = 'New Keyword' Execute("$objFile.SummaryProperties.Keywords = '" & $sValue & "'") Hi PSaltyDS, both of those come back as False, unless the keyword I am assigning is set to the be the same value as $objFile.SummaryProperties.Keywords. D Link to comment Share on other sites More sharing options...
MattyD Posted May 5, 2012 Share Posted May 5, 2012 Sorry to resurect an old topic, no solution - but I've stumbled across some interesting behaviour that may help explain what is going on. I am using Assign and Eval because I am trying to assign a value to a propery. the Execute funciton works only for methods; as someone said earier Execute will perform a comparison when doing this: Execute("$a = 4") To run the following code you will need some dependancies from the native wifi project, but there is probably no need to see what is going on. expandcollapse popup#include "NativeWifi.au3" _Wlan_StartSession() _Test1() _Test2() ;Scenario 1 Func _Test1() ConsoleWrite("-Scenario 1-" & @CRLF) Local $oProfile, $vResult $oProfile = _Wlan_CreateProfileObject() $oProfile.XML = "OldValue" ConsoleWrite("VarGetType:" & VarGetType("oProfile.XML") & @CRLF) ; VarGetType:String ConsoleWrite("IsDeclared:" & IsDeclared("oProfile.XML") & @CRLF) ; IsDeclared:0 ;Because IsDclared() returns 0, It is no supprise these fail... $vResult = Eval("oProfile.XML") ConsoleWrite("Eval: Return=[" & $vResult & "] Error=" & @error & @CRLF) ; Eval: Return=[] Error=1 $vResult = Assign("oProfile.XML", "NewValue", 4) ;4 = Fail if not declared ConsoleWrite("Assign: Return=" & $vResult & " Error=" & @error & @CRLF) ; Assign: Return=0 Error=2 ConsoleWrite(@CRLF) EndFunc ;Scenario 2 Func _Test2() ConsoleWrite("-Scenario 2-" & @CRLF) Local $oProfile, $vResult $oProfile = _Wlan_CreateProfileObject() $oProfile.XML = "OldValue" ConsoleWrite("VarGetType:" & VarGetType("oProfile.XML") & @CRLF) ; VarGetType:String ConsoleWrite("IsDeclared:" & IsDeclared("oProfile.XML") & @CRLF) ; IsDeclared:0 $vResult = Assign("oProfile.XML", "NewValue") ConsoleWrite("Assign: Return=" & $vResult & " Error=" & @error & @CRLF) ; Assign: Return=1 Error=0 ;Assign has now declared oProfile.XML - so eval will work. ConsoleWrite("IsDeclared:" & IsDeclared("oProfile.XML") & @CRLF) ; IsDeclared:-1 $vResult = Eval("oProfile.XML") ConsoleWrite("Eval: Return=[" & $vResult & "] Error=" & @error & @CRLF) ; Eval: Return=[NewValue] Error=0 ;But in the end, the declared oProfile.XML with Assign is just a new variable. ConsoleWrite($oProfile.XML & @CRLF); Returns OldValue EndFunc maniootek 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now