Search code examples
applescriptduplicatesrepeatparagraphs

How do you look for and copy files if they exist in other profiles with applescript?


I am trying to create an applescript to look in each users profile for crash logs in ~/Library/Logs/DiagnosticReports .. I use the * at the end because there could be .crash .spin .hang files and I need to copy them all to the current desktop into the folder "Test" but am not sure if that was the proper way to do it.

When I try and run it I get

error "Can’t make \"Test.txt\" into type integer." number -1700 from "Test.txt" to integer

try
    do shell script "ls /users >$HOME/Desktop/Users.txt"
    do shell script "mkdir ~/Desktop/Test"
end try

set b to boot volume of (system info)
set u to b & "Users"

set theFiles to paragraphs of "Test.txt"



repeat theFiles times
duplicate file u & theFiles & "~/Library/Logs/DiagnosticReports/Xxxxx*" to "~/Desktop/Test"

end repeat

Thanks all for helping.


Solution

  • Just use do shell script:

    do shell script "mkdir -p ~/Desktop/Test
    cp /Users/*/Library/Logs/DiagnosticReports/* ~/Desktop/Test" with administrator privileges
    

    Or loop through folders of (path to users folder):

    tell application "Finder"
        if exists folder "Test" of desktop then
            set d to folder "Test" of desktop
        else
            set d to make new folder at desktop with properties {name:"Test"}
        end if
        repeat with f in (get folders of (path to users folder))
            tell contents of f
                if exists folder "DiagnosticReports" of folder "Logs" of folder "Library" then
                    duplicate items of folder "DiagnosticReports" of folder "Logs" of folder "Library" to d
                end if
            end tell
        end repeat
    end tell
    

    If you don't have permissions to read the files of other users, you can run Finder as root with launchctl unload /System/Library/LaunchAgents/com.apple.Finder.plist; sudo /System/Library/CoreServices/Finder.app/Contents/MacOS/Finder.