Export CSV

Summary: Saves the metadata for the selected records in a Comma Separated Values file.
Requires: EagleFiler
Install Location: ~/Library/Scripts/Applications/EagleFiler/
Last Modified: 2019-10-02

Description

This script exports the selected records’ metadata in a standard CSV format that’s suitable for importing into a spreadsheet (e.g. Excel or Numbers) or database (e.g. FileMaker or Bento).

Installation Instructions · Download in Compiled Format · Download in Text Format

Script

on run
    
set _filename to choose file name with prompt "Export CSV" default name "Metadata.csv"
    
tell application "EagleFiler"
        
set _records to selected records of browser window 1
        
set _file to open for access _filename with write permission
        
set _headerFields to {"Filename", "Path", "Title", "From", "Kind", "Label Index", "Tags", "Source URL", "Notes"}
        
set _headerLine to my lineFromFields(_headerFields)
        
write _headerLine to _file
        
repeat with _record in _records
            
set _line to my lineFromFields(my fieldsFromRecord(_record))
            
write _line to _file as «class utf8»
        
end repeat
        
close access _file
    
end tell
end run

on lineFromFields(_fields)
    
set _quotedFields to {}
    
repeat with _field in _fields
        
copy my quoteCSV(_field) to end of _quotedFields
    
end repeat
    
set lf to ASCII character 10
    
return my join(_quotedFields, ",") & lf
end lineFromFields

on fieldsFromRecord(_record)
    
tell application "EagleFiler"
        
set _fields to {}
        
copy _record's filename to end of _fields
        
set _file to _record's file
        
set _path to _file's POSIX path
        
copy _path to end of _fields
        
copy _record's title to end of _fields
        
copy _record's from name to end of _fields
        
copy _record's kind to end of _fields
        
copy _record's label index as string to end of _fields
        
set _tagNames to {}
        
set _tags to _record's assigned tags
        
repeat with _tag in _tags
            
copy _tag's name to end of _tagNames
        
end repeat
        
copy my join(_tagNames, ", ") to end of _fields
        
copy _record's source URL to end of _fields
        
copy _record's note text to end of _fields
        
return _fields
    
end tell
end fieldsFromRecord

on quoteCSV(_string)
    
set dq to "\""
    
return dq & my replace(_string, dq, dq & dq) & dq
end quoteCSV

on replace(_string, _source, _replacement)
    
set AppleScript's text item delimiters to _source
    
set _items to every text item of _string
    
set AppleScript's text item delimiters to _replacement
    
return _items as Unicode text
end replace

on join(_list, _sep)
    
set _temp to AppleScript's text item delimiters
    
set AppleScript's text item delimiters to _sep
    
set _result to _list as Unicode text
    
set AppleScript's text item delimiters to _temp
    
return _result
end join