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


I recommend you create a Service/Fast Motion for Finder and use a shortcut as an alternative. It’ll verify the presently chosen recordsdata and add them to your pc in a comma-separated values (CSV) file. Everytime you press the shortcut, it can rely the chosen recordsdata, replace the .csv, and append the filenames to the log. Lastly, you need a notification that will help you verify if the method is working. Some error dealing with can also 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 by 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 recordsdata from Finder
    inform utility "Finder"
        activate
        attempt
            set selectedFiles to choice as alias checklist
            if (rely selectedFiles) ≤ 0 then
                show notification "No recordsdata chosen." with title "Error"
                return
            finish if
        on error
            show notification "Error accessing chosen recordsdata in Finder."
            return
        finish attempt
    finish inform
    
    -- Initialize the checklist of logged recordsdata and whole rely
    set loggedFiles to {}
    set totalCount to 0
    
    -- Try to learn current CSV knowledge and rely
    attempt
        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 rely 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 attempt
    
    -- Examine every chosen file towards logged recordsdata
    set newFiles to {}
    repeat with aFile in selectedFiles
        attempt
            -- Convert the Finder choice to a file alias and get its title
            set filePath to POSIX path of (aFile as textual content)
            inform utility "System Occasions"
                set fileName to call of disk merchandise filePath
            finish inform
            
            -- Examine if the file is already logged
            set finish of newFiles to fileName
            set totalCount to totalCount + 1 -- Increment whole rely for every new file
        on error errMsg quantity errNum
            show dialog "Error processing a file: " & errMsg & " (Error Quantity: " & errNum & ")"
        finish attempt
    finish repeat
    
    -- Replace the CSV file with the brand new recordsdata
    if (rely newFiles) > 0 then
        attempt
            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 attempt
    finish if
    
    -- Notify the person of completion with the up to date whole rely
    show notification "Added " & (rely of newFiles) & " new recordsdata. Complete recordsdata: " & totalCount with title "Replace Full"
finish run

Earlier than working the Service, you need to allow Privateness and Safety entry. Embody Finder, Automator and no matter different purposes you might be utilizing. Additionally, be sure you have writing permissions for the CSV.

Recent Articles

Related Stories

Leave A Reply

Please enter your comment!
Please enter your name here