0.3 C
United States of America
Thursday, January 16, 2025

macos – Can I Make Applescript to Depend Variety of Recordsdata Created?


I counsel you create a Service/Fast Motion for Finder and use a shortcut as an alternative. It would examine the at present chosen information and add them to your laptop in a comma-separated values (CSV) file. Everytime you press the shortcut, it can depend the chosen information, replace the .csv, and append the filenames to the log. Lastly, you desire a notification that will help you examine if the method is working. Some error dealing with can be suggested.

To create a Service that runs this script:

  1. Open Automator and create a brand new Service or Fast Motion.
  2. Set the service to obtain ‘no enter’ in ‘Finder.’
  3. Add a ‘Run AppleScript’ motion and paste the script into it.
  4. Save the service and assign a shortcut to it via System Preferences >
    Keyboard > Shortcuts > Providers.

Replace the primary line with the username and path you want to use:

property csvPath : "Macintosh HD:Customers:Username:Desktop:file_log.csv" -- Replace with the precise path to your CSV file

on run
    -- Attempt to get the chosen information from Finder
    inform software "Finder"
        activate
        strive
            set selectedFiles to choice as alias listing
            if (depend selectedFiles) ≤ 0 then
                show notification "No information chosen." with title "Error"
                return
            finish if
        on error
            show notification "Error accessing chosen information in Finder."
            return
        finish strive
    finish inform
    
    -- Initialize the listing of logged information and complete depend
    set loggedFiles to {}
    set totalCount to 0
    
    -- Try to learn current CSV information and depend
    strive
        set fileRef to open for entry file csvPath
        set eofPosition to get eof fileRef
        if eofPosition > 0 then
            set fileContents to learn fileRef
            set loggedFiles to paragraphs of fileContents
            set totalCount to depend of loggedFiles
        finish if
        shut entry fileRef
    on error errMsg quantity errNum
        -- Deal with particular file errors
        shut entry fileRef -- Guarantee file is closed after error
        if errNum is -43 then -- File not discovered
            -- No file discovered, proceed as if it is a new log
        else
            show dialog "Error studying from CSV file: " & errMsg
            return
        finish if
    finish strive
    
    -- Verify every chosen file towards logged information
    set newFiles to {}
    repeat with aFile in selectedFiles
        strive
            -- Convert the Finder choice to a file alias and get its identify
            set filePath to POSIX path of (aFile as textual content)
            inform software "System Occasions"
                set fileName to call of disk merchandise filePath
            finish inform
            
            -- Verify if the file is already logged
            set finish of newFiles to fileName
            set totalCount to totalCount + 1 -- Increment complete depend for every new file
        on error errMsg quantity errNum
            show dialog "Error processing a file: " & errMsg & " (Error Quantity: " & errNum & ")"
        finish strive
    finish repeat
    
    -- Replace the CSV file with the brand new information
    if (depend newFiles) > 0 then
        strive
            set fileRef to open for entry file csvPath with write permission
            -- Transfer to finish of file to append as an alternative of overwriting
            set eof of fileRef to (get eof fileRef)
            repeat with aFile in newFiles
                write aFile & return to fileRef beginning at eof
            finish repeat
            shut entry fileRef
        on error errMsg
            show dialog "Error updating file: " & errMsg
        finish strive
    finish if
    
    -- Notify the consumer of completion with the up to date complete depend
    show notification "Added " & (depend of newFiles) & " new information. Complete information: " & totalCount with title "Replace Full"
finish run

Earlier than working the Service, you could allow Privateness and Safety entry. Embrace Finder, Automator and no matter different functions you might be utilizing. Additionally, ensure you have writing permissions for the CSV.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles