Memecahkan masalah skrip tangkapan layar

0

LATAR BELAKANG

Sebuah Script automator untuk menyimpan tangkapan layar ke file dikembangkan (oleh seseorang yang lebih pintar dari diriku). Untuk beberapa alasan itu tidak andal (jarang) menyimpan tangkapan layar ke file. Kode disajikan di bawah ini.

Kursor tangkapan layar secara konsisten melakukan ketika dipanggil: tangkapan layar ditangkap ke memori. Sebaliknya, tangkapan layar tidak secara konsisten disimpan ke file seperti kode di bawah ini

PERTANYAAN

  • Bagaimana akar penyebab masalah simpan dapat didiagnosis?
  • Apakah ada bug yang jelas dalam kode yang menyebabkan masalah simpan?
  • Apakah ada masalah izin? Bagaimana cara mengkonfirmasi / menguji?

KODE

on run {input, parameters}

    --  # Screen Shot to Clipboard and File

    --  # Clear the clipboard so the 'repeat until isReady ...' loop works properly.

    set the clipboard to ""

    --  # Copy picture of selected area to the clipboard, press: ⌃⇧⌘4
    --  # Note that on my system I need to keystroke '$' instead of '4'.
    --  # I assume this is because the 'shift' key is being pressed.        

    tell application "System Events"
        keystroke "$" using {control down, shift down, command down}
    end tell

    --  # Wait while user makes the selection and releases the mouse or times out.
    --  # Note that the time out also acts as an escape key press of sorts. In other
    --  # words, if the user actually presses the escape key it has no effect on this
    --  # script like it would if pressing the normal shortcut outside of the script.
    --  #       
    --  # As coded, the time out is 5 seconds. Adjust 'or i is greater than 10' and or  
    --  # 'delay 0.5' as appropriate for your needs to set a different length time out.
    --  # This means, as is, you have 5 seconds to select the area of the screen you
    --  # want to capture and let go of the mouse button, otherwise it times out.

    set i to 0
    set isReady to false
    repeat until isReady or i is greater than 10
        delay 0.5
        set i to i + 1
        set cbInfo to (clipboard info) as string
        if cbInfo contains "class PNGf" then
            set isReady to true
        end if
    end repeat
    if not isReady then
        --  # User either pressed the Esc key or timed out waiting.
        return --  # Exit the script without further processing.
    end if

    --  # Build out the screen shot path filename so its convention is of 
    --  # the default behavior when saving a screen shot to the Desktop.

    set theDateTimeNow to (do shell script "date \"+%Y-%m-%d at %l.%M.%S %p\"")
    set theFilename to "Screen Shot " & theDateTimeNow & ".png"
    --  # set thePathFilename to POSIX path of (path to desktop folder as string) & theFilename
    set thePathFilename to "/Users/user/Documents/Captures/" & theFilename

    --  # Retrieve the PNG data from the clipboard and write it to a disk file.

    set pngData to the clipboard as «class PNGf»
    delay 0.5
    try
        set fileNumber to open for access thePathFilename with write permission
        write pngData to fileNumber
        close access fileNumber
    on error eStr number eNum
        try
            close access fileNumber
        end try
        activate
        display dialog eStr & " number " & eNum buttons {"OK"} default button 1 with title "File I/O Error..." with icon caution
    end try

    --  # Hide the file extension as is the default.
    --  # Convert the POSIX path filename to an alias.

    set thePathFilename to POSIX file thePathFilename
    set thePathFilename to thePathFilename as alias
    tell application "Finder"
        try
            set extension hidden of thePathFilename to true
        end try
    end tell


    return input
end run
gatorback
sumber
1
Saya bingung mengapa Anda membutuhkan ini. Ada sebuah Penemu pintasan untuk mengambil tangkapan layar dari suatu pilihan dan meletakkannya di clipbard; tetapi ada juga jalan pintas untuk mengambil tangkapan layar dari suatu pilihan dan menyimpannya langsung ke file di folder tangkapan layar default Anda. Kenapa naskahnya?
CJK
Tentu saja ada lebih dari satu cara untuk 'menguliti kucing', namun demikian, pertanyaannya tetap. Mendokumentasikan opsi tangkapan layar tambahan ke pos asli (tautan dalam kalimat pertama) yang berisi skrip akan meningkatkan pengetahuan.
gatorback
Untuk mendiagnosis masalah dalam skrip, hapus terlebih dahulu baris apa saja di Editor Skrip itu kata try atau end try. Di mana ada on error blokir, hapus semuanya on error dan end try (Anda dapat mengomentari semua baris ini daripada menghapusnya). Kemudian jalankan kembali skrip (dalam Editor Skrip ) dan lihat kesalahan apa yang dilaporkan kembali (panel hasil ada di bagian bawah jendela pengeditan: pilih Balasan daripada Hasil ).
CJK

Jawaban:

1

Untuk kepentingan pembaca lain, perlu dicatat Penemu telah memiliki pintasan keyboard yang memungkinkan Anda untuk:

  • 3 : ambil tangkapan layar dari suatu pilihan dan simpan ke clipboard ;
  • 4 : ambil tangkapan layar dari suatu pilihan dan simpan ke file .

Anda dapat memodifikasi pintasan ini di Preferensi Sistem & gt; Keyboard & gt; Pintasan & gt; Tangkapan Layar .

Saya telah memberikan saran di komentar tentang cara mulai mendiagnosis skrip asli. Tapi, karena saya akan menulis skrip sedikit berbeda jika saya ingin (untuk beberapa alasan) untuk menghindari penggunaan builtin Penemu pintasan keyboard, saya melanjutkan dan menulisnya:

    set screenshots to do shell script "defaults read com.apple.screencapture location"
    set timestamp to do shell script "date +'%Y-%m-%d at %H.%M.%S'"
    set type to "jpg" -- or "png"
    set filename to ["Screen Shot ", timestamp, ".", type] as text

    do shell script "SCREENCAPTURE -ioac -t " & type
    set [imgdata] to (the clipboard) as list

    tell application "Finder"
        set screenshot to (make new file ¬
            at POSIX file screenshots as alias ¬
            with properties {name:filename}) as alias

        write the imgdata as class of imgdata to the screenshot

        reveal the screenshot
    end tell

Sistem Informasi: Versi AppleScript : "2.7", versi sistem : "10.13.4"

Untuk satu, skrip lebih pendek, dan skrip pendek menyediakan lebih sedikit tempat di mana terjadi kesalahan. Saat ini, skrip ini berfungsi dengan baik di sistem saya. Oleh karena itu, Anda dapat mencobanya pada Anda sendiri apakah itu berhasil; jika tidak, Editor Skrip akan melaporkan di mana kesalahan terjadi dan apa kesalahannya, mis. apakah itu kesalahan izin file, dll.

CJK
sumber