Jump to content

Pass object property as a variable


Recommended Posts

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

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

:mellow:

Edit: Forgot COM error handler.

Edited by PsaltyDS
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

  • Moderators

PsaltyDS,

BZ. :mellow:

Another little nugget to store away. :(

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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

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"

:mellow:

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

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"

:mellow:

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

Global $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 by RagsRevenge
Link to comment
Share on other sites

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

$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

Did you try:

Execute("$objFile.SummaryProperties.Keywords = 'New Keyword'")

Or:

$sValue = 'New Keyword'
Execute("$objFile.SummaryProperties.Keywords = '" & $sValue & "'")

:mellow:

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

Did you try:

Execute("$objFile.SummaryProperties.Keywords = 'New Keyword'")

Or:

$sValue = 'New Keyword'
Execute("$objFile.SummaryProperties.Keywords = '" & $sValue & "'")

:mellow:

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

  • 2 years later...

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.

#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
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...