Come faccio a passare i valori delle variabili tra successive esecuzioni di mele? (valore persistente tra le esecuzioni di script)

1

Ho un AppleScript che eseguo come un cron job. All'interno di questo applescript, ho variabili che si comportano come interruttori booleani (cioè valori on / off). Con le esecuzioni successive, a seconda che i valori delle variabili booleane siano accesi o spenti, è necessario intraprendere determinate azioni.

D) Quali sono i diversi modi in cui ciò può essere fatto? (apprezzerebbe il codice di esempio).

Saluti.

Risposta) I file plist o property sembrano essere la strada da percorrere.

credito a daviesgeek per avermi indicato nella giusta direzione. per quelli interessati ho scritto sub-routine helper come segue:

-- read the plist file
tell application "System Events"
   tell property list file plistfile_path
      tell contents
         set someLocalVariable to value of property list item "someLocalVariable"
      end tell
   end tell
end tell


on create_plist_file(file_path)
    -- create the plist file
    tell application "System Events"
         -- create an empty property list dictionary item
        set the parent_dictionary to make new property list item with properties {kind:record}

        -- create new property list file using the empty dictionary list item as contents
        set this_plistfile to ¬
            make new property list file with properties {contents:parent_dictionary, name:file_path}
            -- add new property list items of each of the supported types
            make new property list item at end of property list items of contents of this_plistfile ¬
                with properties {kind:boolean, name:"someLocalVariable", value:false}
            -- make new property list item at end of property list items of contents of this_plistfile ¬
            --     with properties {kind:string, name:"stringKey", value:"string value"}
    end tell
end create_plist_file



on set_someLocalVariable(bool_val)
    tell application "System Events"
        tell property list file plistfile_path
            tell contents
                set value of property list item "someLocalVariable" to bool_val
            end tell
        end tell
    end tell
end set_wasAllTransfersStarted
    
posta Kaushik Gopal 27.07.2012 - 23:53
fonte

2 risposte

2

Puoi scrivere su un file .plist come tale:

--Empty preference file
set theEmptyPListData to "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
<dict/>
</plist>"

--set preference list path and open with write permissions
set theOutputFolder to path to preferences folder from user domain as string
set thePListPath to theOutputFolder & "AppleScript.plist"
set thePListFile to open for access thePListPath with write permission

--check to see if the plist variable is set
try
    tell application "System Events"
        tell property list file thePListPath
            tell contents
                value of property list item "browser"
            end tell
        end tell
    end tell
on error
    tell application "System Events"
        if not (exists file thePListFile) then
            set webBrowser to the button returned of (display dialog "What web browser to you want to use?
(Choose carefully, as this will not appear again)" buttons {"Safari", "Firefox", "Google Chrome"})

            set eof of thePListFile to 0
            write theEmptyPListData to thePListFile starting at eof

            tell property list file thePListPath
                tell contents
                    set value to {|browser|:webBrowser}
                end tell
            end tell
        end if
    end tell
end try

Questo script chiederà il browser dell'utente e lo salverà attraverso le esecuzioni dello script.

    
risposta data 28.07.2012 - 00:24
fonte
0

Di solito creo solo file di testo in ~/Library/Caches/ o /tmp/ .

set prev to do shell script "d=~/Library/Caches/me.lri.scripts; mkdir -p $d; touch $d/example; cat $d/example"
set prev to prev & "aa"
do shell script "shopt -u xpg_echo; echo -n " & quoted form of prev & " > ~/Library/Caches/me.lri.scripts/example"
do shell script "cat ~/Library/Caches/me.lri.scripts/example" without altering line endings
    
risposta data 29.07.2012 - 17:50
fonte

Leggi altre domande sui tag