6 Replies Latest reply on Mar 17, 2015 9:43 AM by Dirk Becker

    [AS] storing records in the document

    David André Erichsen Level 1

      Im trying to store some PDF-export settings in the document as a label key string.

       

      1) How do I convert the record to one string?

      2) How do I convert the string back to a record?


      I want the return-value in this code to be "Value2".

       

       

      set myPDFsettingRecord to {PDFsettingA:"value1", PDFsettingB:"value2", PDFsettingC:"value3"}

       

      tell application "Adobe InDesign CC 2014"

        tell active document

        -- store string in document

        insert label key "PDFsettings" value myPDFsettingRecord as string

        -- read string in document

        set PDFsettings to extract label key "PDFsettings"

        end tell

      end tell

      set PDFsettings to split(PDFsettings, ",")

       

      return PDFsettingA of PDFsettings

       

       

      to split(someText, delimiter)

        set AppleScript's text item delimiters to delimiter

        set someText to someText's text items

        set AppleScript's text item delimiters to {""}

        return someText

      end split

        • 1. Re: [AS] storing records in the document
          Ronald63 Level 4

          Hello,

           

          Try this ...

          set theRecord to {PDFsettingA:"value1", PDFsettingB:"value2", PDFsettingC:"value3", PDFsettingD:"value4", PDFsettingE:"value5"}
          set mString to recordToString(theRecord)
          
          set mResult to item 2 of split(item 2 of split(mString, ","), ":")
          
          on recordToString(theRecord)
              try
                  return theRecord as text
              on error theError
                  set tids to text item delimiters
                  set text item delimiters to "{"
                  set a to text item 2 of theError
                  set text item delimiters to "}"
                  set b to text item 1 of a
                  set text item delimiters to "\""
                  set c to text items of b
                  set text item delimiters to ""
                  set d to "{" & (c as text) & "}"
                  set text item delimiters to tids
                  return d
              end try
          end recordToString
          
          to split(someText, delimiter)
              set AppleScript's text item delimiters to delimiter
              set someText to someText's text items
              set AppleScript's text item delimiters to {""}
              return someText
          end split
          
          • 2. Re: [AS] storing records in the document
            David André Erichsen Level 1

            Thank you for helping me!

             

            As long as I can see, this convert the record to one string.

            But it does not convert it back in a variable as a record?

            • 3. Re: [AS] storing records in the document
              Ronald63 Level 4

              Hi,

              set mRecordKey to item 2 of split(item 1 of split(mString, ","), ":")
              set mRecordValue to item 2 of split(item 2 of split(mString, ","), ":")
              
              set mResult to {mRecordKey:mRecordValue}
              
              • 4. Re: [AS] storing records in the document
                Loic.Aigon Adobe Community Professional

                Hi,

                Why not use a call to js in order to take advantage of the toSource() function ?

                 

                
                tell application "Adobe InDesign CC 2014"
                  set js to "app.pdfExportPresets[0].properties.toSource()"
                  set props to do script js language javascript
                  display dialog "Stored:" & props
                  set js2 to "var props = eval(" & props & "); alert(props.reflect.properties);"
                 do script js2 language javascript
                end tell
                

                 

                FWIW,

                 

                Loic

                http://www.ozalto.com

                • 5. Re: [AS] storing records in the document
                  David André Erichsen Level 1

                  I finally got it right:

                  Thank you guys for helping me out!

                  -- list of record to store in document:

                  set theRecord to {PDFsettingA:"value1", PDFsettingB:"value2", PDFsettingC:"value3", PDFsettingD:"value4", PDFsettingE:"value5"}

                  -- Set records to one string

                  set mString to recordToString(theRecord)

                   

                  -- save string in active InDesign-document

                  tell application "Adobe InDesign CC 2014"

                    tell active document

                    insert label key "PDFsettings" value mString as string

                    end tell

                  end tell

                   

                  -- return string in active InDesign-document

                  tell application "Adobe InDesign CC 2014"

                    tell active document

                    set mStringReturn to extract label key "PDFsettings"

                    end tell

                  end tell

                   

                  --removes Curly Bracets from string and make a list of it

                  set mStringReturn to split((text 2 thru ((number of items in text of mStringReturn) - 1) of mStringReturn), ", ")

                   

                  -- split the list into records

                  repeat with i from 1 to the number of items in mStringReturn

                    set x to split((item i of mStringReturn), ":")

                    if i is equal to 1 then

                    set mStringNew to {(item 1 of x), (item 2 of x)}

                    else

                    set mStringNew to mStringNew & {(item 1 of x), (item 2 of x)}

                    end if

                  end repeat

                  set myRecords to listToRecord(mStringNew)

                   

                  return myRecords

                   

                   

                   

                   

                  -- Handlers:

                  on recordToString(theRecord)

                    try

                    return theRecord as text

                    on error theError

                    set tids to text item delimiters

                    set text item delimiters to "{"

                    set a to text item 2 of theError

                    set text item delimiters to "}"

                    set b to text item 1 of a

                    set text item delimiters to "\""

                    set c to text items of b

                    set text item delimiters to ""

                    set d to "{" & (c as text) & "}"

                    set text item delimiters to tids

                    return d

                    end try

                  end recordToString

                   

                  on split(someText, delimiter)

                    set AppleScript's text item delimiters to delimiter

                    set someText to someText's text items

                    set AppleScript's text item delimiters to {""}

                    return someText

                  end split

                   

                  on listToRecord(L)

                    script myScript

                    returnclass usrf»:L}

                    end script

                    return run script myScript

                  end listToRecord

                  • 6. Re: [AS] storing records in the document
                    Dirk Becker Level 4

                    @Loic - toSource() is broken for enums since CS5.

                     

                    @David

                    Be careful with text items, some separator could also be contained in a string.

                    That listToRecord is an interesting approach.