Tuesday, September 24, 2013

AppleScript: getting and setting user data

In the Properties > Note inspector, you can set user data of an object. Each user-data item is a name/value pair. For example, in the image below, the item "oName" of the selected object is set to "drawer," and the item "drawerState" is set to "open."


Using the name of the data item, you can set and retrieve its value in AppleScript. User data is useful when scripting as it's a way of storing values for future use after the script has completed its execution.

Here are two useful methods for getting and setting user-data items of an object. Note that the value set or retrieved is a string.

-- Returns value of user-data item itemName of graphic obj.
-- Returns null if data item doesn't exist, or obj is invalid.

on getUserDataItem(obj, itemName)
   tell application id "OGfl"
      try
         set retval to value of user data item itemName of obj
         -- force evaluation of retval to trigger the error:
         set triggerError to retval 
      on error
         return null
      end try
      return retval
   end tell
end getUserDataItem

-- Sets user-data item itemName of graphic obj to val,
-- adds that user-data item if it doesn't exist.
-- Returns null if obj is an invalid, or value failed to set 
-- (e.g., val isn't a string); else returns passed value.

on setUserDataItem(obj, itemName, val)
   tell application id "OGfl"
      try
         set value of user data item itemName of obj to val
      on error
         return null
      end try
      return val
   end tell
end setUserDataItem

1 comment:

  1. The construction of iconic landmarks, such as the Burj Khalifa in Dubai or the One World Trade Center in New York City, not only redefine skylines but also serve as symbols of human ambition and achievement. The interplay between real estate development and city planning is a constant balancing act that shapes the way we live, work, and interact with our surroundings. Hillock Green Condo

    ReplyDelete