Cara Memiliki Tindakan Kustom Terletak Di Dalam Applescript.app, Jalankan Hanya Pada Setiap Peluncuran Aplikasi Kelima

5

Oke, inilah situasiku. Saya telah membuat skrip yang fungsinya untuk mencadangkan file skrip saya yang saya simpan ke folder iCloud Drive / Script Editor, ke dua lokasi berbeda. Setiap lokasi berada dalam folder bernama "JIMZ_Scriptz" pada dua Kapsul Waktu yang berbeda. Awalnya saya menggandakan setiap item yang terletak di folder iCloud Drive / Script Editor saya ke "JIMZ_Scriptz" di kedua Kapsul Waktu saya. Karena setiap file sudah digandakan, fungsi skrip saya sekarang adalah hanya menduplikasi file folder iCloud drive / Editor Skrip saya, yang tanggal pembuatannya dalam tiga hari terakhir dan siapa tanggal modifikasi dalam tiga hari terakhir ke " JIMZ_Scriptz ”di kedua Kapsul Waktu saya

Dalam proses penelitian dan pembelajaran saya, saya menemukan ... bagaimana memiliki AppleScript ubah nilai propertinya yang telah ditetapkan berdasarkan berapa kali skrip dijalankan . Sebagai contoh, saya memiliki skrip ini mengatur bahwa setiap menjalankan kelima, itu akan menetapkan nilai properti ini ... property archiveMode : missing value ... benar. Selanjutnya saya memiliki set kondisional dalam skrip ini bahwa jika archiveMode benar, maka ia akan melakukan tindakan yang saya tetapkan dalam pernyataan bersyarat.

Tapi inilah tangkapannya. Selama skrip tidak bisa dikompilasi ulang dan disimpan kembali, dengan setiap kali skrip dijalankan di Editor Skrip, skrip akan terus mengubah nilai dalam property number_of_runs : 0 dari 1 hingga 2 hingga 3 dan seterusnya. Saat dikompilasi dan disimpan sebagai aplikasi dan aplikasi itu dijalankan di luar Script Editor, itu kehilangan kemampuan untuk menyimpan dan menyimpan nilai baru ke properti dan variabel . Saat ini saya telah menetapkan properti dengan cara yang setiap kali saya meluncurkan aplikasi yang dikompilasi, itu mengatur archiveMode menjadi true, menampilkan dialog yang menanyakan apakah saya ingin menjalankan tindakan tertentu.

T: Bagaimana Agar Tindakan Kustom Berlokasi Di Dalam Applescript.app, Hanya Menjalankan Pada Setiap Peluncuran Kelima Aplikasi?

property inputPath1 : alias "Macintosh HD:Users:Smokestack:Library:Mobile Documents:com~apple~ScriptEditor2:Documents:"
property inputPath2 : alias "Macintosh HD:Users:Smokestack:Library:Script Libraries:"
property inputPath3 : alias "Macintosh HD:Users:Smokestack:Library:Workflows:Applications:Folder Actions:"
property destinationOne : "Data_Smokestack_ATC:JIMZ_Storage_Filez:JIMZ_Scriptz"
property destinationTwo : "Data_Jimmy's_ATC:Jimz_Filez:JIMZ_Storage_Filez:JIMZ_Scriptz:"
property modiedDate : (current date) - (days * 3)
property creationDate : (current date) - (days * 3)
property archiveMode : missing value
property zipMe1 : POSIX path of "/Volumes/Data_Smokestack_ATC/JIMZ_Storage_Filez/JIMZ_Scriptz"
property zipMe2 : POSIX path of "/Volumes/Data_Jimmy's_ATC/Jimz_Filez/JIMZ_Storage_Filez/JIMZ_Scriptz"
property number_of_runs : 0
property number_of_files : 0

set number_of_runs to number_of_runs + 1

if number_of_runs = 5 then
    set archiveMode to true
end if

stopTimeMachine()

set MountdestinationOne to mount volume "afp://Jimmy's AirPort Time Capsule._afpovertcp._tcp.local/Data_Jimmy's_ATC"
set MountdestinationTwo to mount volume "afp://Smokestack AirPort Time Capsule._afpovertcp._tcp.local/Data_Smokestack_ATC"

if archiveMode is true then
    try
        set dialogAnswer to display dialog ¬
            "Would You Like To Create An Archive Of Your JIMZ_Scriptz Folders In Both Of Your Time Capsules?" buttons {"Yes", "No", "Cancel"} ¬
            default button "Yes"
        if button returned of dialogAnswer is "Yes" then
            do shell script "zip -r " & quoted form of zipMe1 & " " & quoted form of zipMe1
            tell application "Finder"
                set moveZipFile to POSIX file "/Volumes/Data_Smokestack_ATC/JIMZ_Storage_Filez/JIMZ_Scriptz.zip"
                set moveZipFile to moveZipFile as alias
                set moveZipFile2 to alias "Data_Jimmy's_ATC:Jimz_Filez:JIMZ_Storage_Filez:Zipped_Jimz_Scriptz:"
                set moveZipFileTo to alias "Data_Smokestack_ATC:JIMZ_Storage_Filez:Zipped_Jimz_Scriptz:"
                set number_of_files to count (items of folder moveZipFileTo)
                set number_of_files to number_of_files + 1
                set new_zip_name to "JIMZ_Scriptz " & number_of_files & ".zip"
                set name of moveZipFile to new_zip_name
                set resultObject to move moveZipFile to moveZipFileTo without replacing
                set resultObject2 to duplicate resultObject to moveZipFile2
            end tell
            set archiveMode to false
        else
            backupScriptz()
        end if
    on error number -128 -- userCanceledErr
        stopTimeMachine()
        return
    end try
    set archiveMode to false
end if

backupScriptz()

stopTimeMachine()

on backupScriptz()
    tell application "Finder"
        try
            with timeout of 280 seconds
                duplicate (every item of inputPath1 whose modification date > modiedDate) to destinationOne with replacing
                duplicate (every item of inputPath1 whose creation date > creationDate) to destinationOne with replacing
                duplicate (every item of inputPath2 whose modification date > modiedDate) to destinationOne with replacing
                duplicate (every item of inputPath2 whose creation date > creationDate) to destinationOne with replacing
                duplicate (every item of inputPath3 whose modification date > modiedDate) to destinationOne with replacing
                duplicate (every item of inputPath3 whose creation date > creationDate) to destinationOne with replacing
            end timeout
            with timeout of 280 seconds
                duplicate (every item of inputPath1 whose modification date > modiedDate) to destinationTwo with replacing
                duplicate (every item of inputPath1 whose creation date > creationDate) to destinationTwo with replacing
                duplicate (every item of inputPath2 whose modification date > modiedDate) to destinationTwo with replacing
                duplicate (every item of inputPath2 whose creation date > creationDate) to destinationTwo with replacing
                duplicate (every item of inputPath3 whose modification date > modiedDate) to destinationTwo with replacing
                duplicate (every item of inputPath3 whose creation date > creationDate) to destinationTwo with replacing
            end timeout
        on error the error_message number the error_number
            set the error_text to "Error: " & the error_number & ". " & the error_message
            my write_error_log(the error_text)
        end try
    end tell
end backupScriptz

on write_error_log(this_error)
    set the error_log to ((path to desktop) as text) & "Jimz_Time_Capsule_Backup Error Log.txt"
    try
        open for access file the error_log with write permission
        write (this_error & " " & (current date) & return) to file the error_log starting at eof
        close access file the error_log
    on error
        try
            close access file the error_log
        end try
    end try
end write_error_log

on stopTimeMachine()
    tell application "System Preferences"
        reveal anchor "main" of pane "com.apple.prefs.backup"
        tell application "System Events"
            perform action "AXPress" of checkbox "Back Up Automatically" of window "Time Machine" of application process "System Preferences"
        end tell
    end tell
    tell application "System Preferences" to quit
end stopTimeMachine

PEMBARUAN: Ini Versi Kode Revisi. Kode yang Diperbarui Ini Memang Akan Melakukan Tindakan Yang Berlokasi Dalam Klausa Bersyarat Pada Setiap Peluncuran Kelima Aplikasi.

Pendekatan yang saya ambil adalah menggunakan perintah "menulis ke file". Setiap kali aplikasi skrip diluncurkan, ia menulis kata "Diluncurkan" ke file teks eksternal. Kemudian saya menggunakan perintah "baca file" untuk menghitung jumlah kata "Diluncurkan" dalam file teks. Selanjutnya saya menyalin hitungan ke variabel dan jika variabel itu = 5 memicu tindakan yang sesuai dan menghapus file teks eksternal, yang memulai siklus kembali dari awal

property inputPath1 : alias "Macintosh HD:Users:Smokestack:Library:Mobile Documents:com~apple~ScriptEditor2:Documents:"
property inputPath2 : alias "Macintosh HD:Users:Smokestack:Library:Script Libraries:"
property inputPath3 : alias "Macintosh HD:Users:Smokestack:Library:Workflows:Applications:Folder Actions:"
property destinationOne : "Data_Smokestack_ATC:JIMZ_Storage_Filez:JIMZ_Scriptz"
property destinationTwo : "Data_Jimmy's_ATC:Jimz_Filez:JIMZ_Storage_Filez:JIMZ_Scriptz:"
property modiedDate : (current date) - (days * 3)
property creationDate : (current date) - (days * 3)
property archiveMode : missing value
property zipMe1 : POSIX path of "/Volumes/Data_Smokestack_ATC/JIMZ_Storage_Filez/JIMZ_Scriptz"
property zipMe2 : POSIX path of "/Volumes/Data_Jimmy's_ATC/Jimz_Filez/JIMZ_Storage_Filez/JIMZ_Scriptz"
property number_of_files : 0
property launchCount : 0

writeToFile()

readFile()

if launchCount = 5 then
    set archiveMode to true
    tell application "Finder"
        delete alias "Macintosh HD:private:tmp:Launch_Count.txt"
    end tell
end if

stopTimeMachine()

set MountdestinationOne to mount volume "afp://Jimmy's AirPort Time Capsule._afpovertcp._tcp.local/Data_Jimmy's_ATC"
set MountdestinationTwo to mount volume "afp://Smokestack AirPort Time Capsule._afpovertcp._tcp.local/Data_Smokestack_ATC"

if archiveMode is true then
    try
        set dialogAnswer to display dialog ¬
            "Creating An Archive Of Your JIMZ_Scriptz Folders In Both Of Your Time Capsules?" buttons {"OK", "NO"} ¬
            default button "OK"
        if button returned of dialogAnswer is "OK" then
            do shell script "zip -r " & quoted form of zipMe1 & " " & quoted form of zipMe1
            tell application "Finder"
                set moveZipFile to POSIX file "/Volumes/Data_Smokestack_ATC/JIMZ_Storage_Filez/JIMZ_Scriptz.zip"
                set moveZipFile to moveZipFile as alias
                set moveZipFile2 to alias "Data_Jimmy's_ATC:Jimz_Filez:JIMZ_Storage_Filez:Zipped_Jimz_Scriptz:"
                set moveZipFileTo to alias "Data_Smokestack_ATC:JIMZ_Storage_Filez:Zipped_Jimz_Scriptz:"
                set number_of_files to count (items of folder moveZipFileTo)
                set number_of_files to number_of_files + 1
                set new_zip_name to "JIMZ_Scriptz " & number_of_files & ".zip"
                set name of moveZipFile to new_zip_name
                set resultObject to move moveZipFile to moveZipFileTo with replacing
                set resultObject2 to duplicate resultObject to moveZipFile2
            end tell
            set archiveMode to false
        else
            backupScriptz()
        end if
    end try
    set archiveMode to false
end if

backupScriptz()

stopTimeMachine()

on backupScriptz()
    tell application "Finder"
        try
            with timeout of 280 seconds
                duplicate (every item of inputPath1 whose modification date > modiedDate) to destinationOne with replacing
                duplicate (every item of inputPath1 whose creation date > creationDate) to destinationOne with replacing
                duplicate (every item of inputPath2 whose modification date > modiedDate) to destinationOne with replacing
                duplicate (every item of inputPath2 whose creation date > creationDate) to destinationOne with replacing
                duplicate (every item of inputPath3 whose modification date > modiedDate) to destinationOne with replacing
                duplicate (every item of inputPath3 whose creation date > creationDate) to destinationOne with replacing
            end timeout
            with timeout of 280 seconds
                duplicate (every item of inputPath1 whose modification date > modiedDate) to destinationTwo with replacing
                duplicate (every item of inputPath1 whose creation date > creationDate) to destinationTwo with replacing
                duplicate (every item of inputPath2 whose modification date > modiedDate) to destinationTwo with replacing
                duplicate (every item of inputPath2 whose creation date > creationDate) to destinationTwo with replacing
                duplicate (every item of inputPath3 whose modification date > modiedDate) to destinationTwo with replacing
                duplicate (every item of inputPath3 whose creation date > creationDate) to destinationTwo with replacing
            end timeout
        on error the error_message number the error_number
            set the error_text to "Error: " & the error_number & ". " & the error_message
            my write_error_log(the error_text)
        end try
    end tell
    display notification ¬
        "Your Most Recent Script Files Have Been Backed Up" with title ¬
        "BACKUP COMPLETED" sound name "Bottle"
end backupScriptz

on write_error_log(this_error)
    set the error_log to ((path to desktop) as text) & "Jimz_Time_Capsule_Backup Error Log.txt"
    try
        open for access file the error_log with write permission
        write (this_error & " " & (current date) & return) to file the error_log starting at eof
        close access file the error_log
    on error
        try
            close access file the error_log
        end try
    end try
end write_error_log

on stopTimeMachine()
    tell application "System Preferences"
        reveal anchor "main" of pane "com.apple.prefs.backup"
        try
            tell application "System Events"
                perform action "AXPress" of checkbox "Back Up Automatically" of window "Time Machine" of application process "System Preferences"
            end tell
        end try
    end tell
    tell application "System Preferences" to quit
end stopTimeMachine

on writeToFile()
    set theFile to "/tmp/Launch_Count.txt"
    set theText to "Launched"
    try
        set writeToFile to open for access theFile with write permission
        write theText & linefeed to writeToFile as text starting at eof
        close access theFile
    on error errMsg number errNum
        close access theFile
        set writeToFile to open for access theFile with write permission
        write theText & linefeed to writeToFile as text starting at eof
        close access theFile
    end try
end writeToFile

on readFile()
    set theFile1 to alias "Macintosh HD:private:tmp:Launch_Count.txt"
    set theCount to count words of (read theFile1)
    copy theCount to launchCount
end readFile

PEMBARUAN # 2: Jadi saya mengunduh tambahan skrip yang disebut Satimage , dan ulang kode saya sebagai kombinasi dari perintah dari Satimage (terutama perintah CADANGAN) dikombinasikan dengan beberapa kode saya sebelumnya dari posting UPDATE dalam topik ini.

Ini adalah versi terbaru

T: Adakah yang bisa mencoba menjelaskan kepada saya mengapa versi baru ini hanya membutuhkan waktu 18 detik untuk menyelesaikan, tanpa harus menggunakan klausa batas waktu (tidak termasuk "perintah arsip" pada peluncuran aplikasi yang kelima), sedangkan solusi asli biasanya lebih baik bagian dari 20 menit? Bagaimana penambahan skrip pihak ketiga dapat melakukan 1000 kali lebih cepat daripada perintah skrip Finder.app?

property archiveMode : missing value
property zipMe1 : POSIX path of "/Volumes/Data_Smokestack_ATC/JIMZ_Storage_Filez/JIMZ_Scriptz"
property zipMe2 : POSIX path of "/Volumes/Data_Jimmy's_ATC/Jimz_Filez/JIMZ_Storage_Filez/JIMZ_Scriptz"
property number_of_files : 0
property launchCount : 0

writeToFile()

writeToFile2()

readFile()

if launchCount = 5 then
    set archiveMode to true
    tell application "Finder"
        delete alias "Macintosh HD:private:tmp:Launch_Count.txt"
    end tell
end if

if archiveMode is true then
    try
        set dialogAnswer to display dialog ¬
            "Creating An Archive Of Your JIMZ_Scriptz Folders In Both Of Your Time Capsules?" buttons {"OK", "NO"} ¬
            default button "OK"
        if button returned of dialogAnswer is "OK" then
            do shell script "zip -r " & quoted form of zipMe1 & " " & quoted form of zipMe1
            tell application "Finder"
                set moveZipFile to POSIX file "/Volumes/Data_Smokestack_ATC/JIMZ_Storage_Filez/JIMZ_Scriptz.zip"
                set moveZipFile to moveZipFile as alias
                set moveZipFile2 to alias "Data_Jimmy's_ATC:Jimz_Filez:JIMZ_Storage_Filez:Zipped_Jimz_Scriptz:"
                set moveZipFileTo to alias "Data_Smokestack_ATC:JIMZ_Storage_Filez:Zipped_Jimz_Scriptz:"
                set number_of_files to count (items of folder moveZipFileTo)
                set number_of_files to number_of_files + 1
                set new_zip_name to "JIMZ_Scriptz " & number_of_files & ".zip"
                set name of moveZipFile to new_zip_name
                set resultObject to move moveZipFile to moveZipFileTo with replacing
                set resultObject2 to duplicate resultObject to moveZipFile2
            end tell
            set archiveMode to false
        else
            backupScriptz()
        end if
    end try
    set archiveMode to false
end if

on writeToFile()
    set MountdestinationOne to mount volume "afp://Jimmy's AirPort Time Capsule._afpovertcp._tcp.local/Data_Jimmy's_ATC"
    set MountdestinationTwo to mount volume "afp://Smokestack AirPort Time Capsule._afpovertcp._tcp.local/Data_Smokestack_ATC"
    set theSource to alias "Macintosh HD:Users:Smokestack:Library:Mobile Documents:com~apple~ScriptEditor2:Documents:"
    set theSource2 to alias "Macintosh HD:Users:Smokestack:Library:Script Libraries:"
    set theSource3 to alias "Macintosh HD:Users:Smokestack:Library:Workflows:Applications:Folder Actions:"
    set theDestination2 to alias "Data_Smokestack_ATC:JIMZ_Storage_Filez:JIMZ_Scriptz:"
    set theDestination3 to alias "Data_Jimmy's_ATC:Jimz_Filez:JIMZ_Storage_Filez:JIMZ_Scriptz:"

    set resultText to backup theSource ¬
        onto theDestination2 ¬
        level 2 ¬
        after ((get current date) - 359200)

    set resultText2 to backup theSource2 ¬
        onto theDestination2 ¬
        level 2 ¬
        after ((get current date) - 359200)

    set resultText3 to backup theSource3 ¬
        onto theDestination2 ¬
        level 2 ¬
        after ((get current date) - 359200)

    set resultText4 to backup theSource ¬
        onto theDestination3 ¬
        level 2 ¬
        after ((get current date) - 359200)

    set resultText5 to backup theSource2 ¬
        onto theDestination3 ¬
        level 2 ¬
        after ((get current date) - 359200)

    set resultText6 to backup theSource3 ¬
        onto theDestination3 ¬
        level 2 ¬
        after ((get current date) - 359200)

    set theFile to "/tmp/Files_To_Backup.txt"

    try
        set writeToFile to open for access theFile with write permission
        set theReports to write resultText & resultText2 & resultText3 & resultText4 & resultText5 & resultText6 & " " & (current date) & return to writeToFile as text starting at eof
        close access theFile
    on error errMsg number errNum
        close access theFile
        set writeToFile to open for access theFile with write permission
        set theReports to write resultText & resultText2 & resultText3 & resultText4 & resultText5 & resultText6 & " " & (current date) & return to writeToFile as text starting at eof
        close access theFile
    end try
end writeToFile

on writeToFile2()
    set theFile2 to "/tmp/Launch_Count.txt"
    set theText to "Launched"
    try
        set writeToFile2 to open for access theFile2 with write permission
        write theText & linefeed to writeToFile2 as text starting at eof
        close access theFile2
    on error errMsg number errNum
        close access theFile2
        set writeToFile2 to open for access theFile2 with write permission
        write theText & linefeed to writeToFile2 as text starting at eof
        close access theFile2
    end try
end writeToFile2

on readFile()
    set theFile3 to alias "Macintosh HD:private:tmp:Launch_Count.txt"
    set theCount to count words of (read theFile3)
    copy theCount to launchCount
end readFile
wch1zpink
sumber

Jawaban:

3

Anda perlu menggunakan penghitung yang hidup di luar skrip. Buat file yang menampung jumlah proses.

Anda dapat menambahkan satu titik ke nama file untuk membuat file tidak terlihat.

# https://apple.stackexchange.com/questions/298264/how-to-have-a-custom-action-located-within-an-applescript-app-execute-only-on-e
# perform a command every fifth time this script is run (as an app)

set file_exists to "no"
set file_name to "oa_counter"
set file_path to POSIX path of ((path to home folder)) & file_name
set run_count to 0
tell application "Finder" to if exists file_path as POSIX file then set file_exists to "yes"

if file_exists is "yes" then
    set run_count to do shell script "cat " & POSIX path of file_path # get the current run count from the file
    do shell script "rm " & POSIX path of file_path # remove the file
end if

if run_count + 1 as number is 5 then
    display dialog "5" # do this every fifth time
else
    set run_count to run_count + 1
    do shell script "cat <<EOF >> " & POSIX path of file_path & "
" & run_count # create a new file with the new run count
    return run_count
end if
oa-
sumber
1
Bagus sekali. Bagaimana dengan layanan web yang berjalan dalam wadah dengan layanan mikro untuk memuat status? JK - persistensi data ke file token yang disimpan pada sistem file mungkin adalah solusi upaya yang paling sedikit kecuali Anda ingin meningkatkan defaults write com.foo.whatever untuk tetap menggunakan penghitung untuk penyimpanan jumlah peluncuran.
bmike
1
@ oa- I +1 dan menerima jawaban Anda. Meskipun saya mengambil pendekatan yang sedikit berbeda, jawaban Anda yang membuat saya di jalan yang benar. Kerja bagus, terima kasih!
wch1zpink
3

Anda dapat menggunakan modulo operator:

set number_of_runs to number_of_runs + 1
set archiveMode to number_of_runs mod 5 = 0 -- the result is true on 5, 10, 15, etc..

Dari pada:

set number_of_runs to number_of_runs + 1
if number_of_runs = 5 then
    set archiveMode to true
end if
jackjr300
sumber