Home > Enterprise >  Is there a way to write two lists to a .csv two to separate columns in Apple Script?
Is there a way to write two lists to a .csv two to separate columns in Apple Script?

Time:06-14

Looking to automate writing the filesizes of files/folders immediately beneath a directory to a .csv

du -sh is the closes to what I'm looking for but all the data ends up in one column when writing to a .csv. If it were possible to comma deliminate the data instead of tab, that might solve the issue.

The closes I've gotten is defining two variables: ChildSizes and ChildPaths. With each being only a list of the data I want.

I've tried echo, printf, and now paste without getting exactly what I want and am a bit stumped.

set RootDirectoryPosix to POSIX path of RootDirectoryAlias
set ChildSizes to do shell script ("du -sh -- " & RootDirectoryPosix & "*" & "| cut -f1")
set ChildPaths to do shell script ("du -sh -- " & RootDirectoryPosix & "*" & "| cut -f2")
set ChildPathsString to ChildPaths as string
set ChildSizesString to ChildSizes as string
tell application "Finder"
    do shell script "paste -d ," & ChildSizesString & " " & ChildPathsString & " >~/Test.csv"
end tell

CodePudding user response:

Here, I choose for the root folder the Scripts folder from user domain. Following script creates CSV file on the desktop.

As I checked the shell command creates text delimited with TABs. The script simply replaces them with ";" delimiter.

set rootFolder to path to scripts folder from user domain
set RootDirectoryPosix to quoted form of (POSIX path of rootFolder)

set csvText to do shell script "du -sh -- " & RootDirectoryPosix & "*"

set {TID, text item delimiters} to {text item delimiters, tab}
set csvText to text items of csvText
set AppleScript's text item delimiters to ";"
set csvText to csvText as text
set AppleScript's text item delimiters to TID

set fileRef to open for access file (((path to desktop) as text) & "Desktop CSV file .csv") with write permission
write csvText to fileRef as «class utf8»
close access fileRef

With adding column titles:

set rootFolder to path to scripts folder from user domain
set RootDirectoryPosix to quoted form of (POSIX path of rootFolder)

set csvText to "   The Sizes   ;   The Paths    " & return
set csvText to csvText & (do shell script "du -sh -- " & RootDirectoryPosix & "*")

set {TID, text item delimiters} to {text item delimiters, tab}
set csvText to text items of csvText
set AppleScript's text item delimiters to ";"
set csvText to csvText as text
set AppleScript's text item delimiters to TID

set fileRef to open for access file (((path to desktop) as text) & "Desktop CSV file .csv") with write permission
write csvText to fileRef as «class utf8»
close access fileRef

CodePudding user response:

This is what I ended up with:

set RootDirectoryPosix to quoted form of (POSIX path of rootFolder)

set csvText to "The Sizes,The Paths" & return
set csvText to csvText & (do shell script "du -sh -- " & RootDirectoryPosix & "*")

set {TID, text item delimiters} to {text item delimiters, tab}
set csvText to text items of csvText
set AppleScript's text item delimiters to ","
set csvText to csvText as text
set AppleScript's text item delimiters to TID

set fileRef to open for access file (((path to desktop) as text) & "RootFolderdu-sh.csv") with write permission
write csvText to fileRef as «class utf8»
close access fileRef
  • Related