Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaydisc/513b8791bed3302febb66657f6d60465 to your computer and use it in GitHub Desktop.
Save jaydisc/513b8791bed3302febb66657f6d60465 to your computer and use it in GitHub Desktop.
property excluded_fields : {"Created Date", "Modified Date", "base station name(name)"} --, "full name(name)", "Password"
property original_text_item_delimiters : AppleScript's text item delimiters
property delimiter : tab
on run
set chosen_file to (choose file)
set imported_file to read chosen_file
set AppleScript's text item delimiters to (ASCII character 10)
set rows to every text item of imported_file
set AppleScript's text item delimiters to original_text_item_delimiters
set field_names_text to item 1 of rows
set AppleScript's text item delimiters to delimiter
set field_names to every text item of field_names_text
set AppleScript's text item delimiters to original_text_item_delimiters
-- set current_records to {}
repeat with row_number from 2 to count rows
-- get the row as string
set current_row to item row_number of rows
-- if the row is not empty we can do stuff
if current_row is not "" then
-- initialise values
set current_record_title to null
set current_record_type to null
set current_record_data to {}
set current_record_notes to null
-- get the row as fields
set AppleScript's text item delimiters to delimiter
set fields to every text item of current_row
set AppleScript's text item delimiters to original_text_item_delimiters
--log fields
-- iterate through fields
repeat with field_number from 1 to count fields
set field to item field_number of fields
if field is not "" and field is not "\"\"" then
set field_name to strip_quotes(item field_number of field_names)
--log field_name
if field_name = "Title" then
set current_record_title to clean_field_value(field)
else if field_name = "Type" then
set current_record_type to clean_field_value(field) & "s"
else if field_name = "Notes" then
set current_record_notes to clean_field_value(field)
else if excluded_fields does not contain field_name then
set current_record_data to current_record_data & (clean_field_name(field_name) & ": " & clean_field_value(field))
end if
end if
end repeat
-- create the record and append it
set current_record to {title:current_record_title, type:current_record_type, data:current_record_data, notes:current_record_notes}
--return current_record
--return
-- Prepare the data for Notes
set AppleScript's text item delimiters to "</div><div>"
set current_record_body to "<div>" & (current_record_data as text) & "</div>"
set AppleScript's text item delimiters to original_text_item_delimiters
if current_record_notes is not null then
set current_record_body to current_record_body & "<div><br /></div><div>Notes:</div><div>" & current_record_notes & "</div>"
end if
--log current_record_body
tell application "Notes"
if not (exists folder current_record_type of account "iCloud") then
make new folder at account "iCloud" with properties {name:current_record_type}
end if
if not (exists note current_record_title in folder current_record_type of account "iCloud") then
log "Creating Note"
make new note at folder current_record_type of account "iCloud" with properties {name:current_record_title, body:current_record_body}
else if current_record_type = "Software Licences" then
log "Note exists of type Software Licence: " & (get body of note current_record_title in folder current_record_type of account "iCloud")
set current_record_body to body of note current_record_title in folder current_record_type of account "iCloud" & "<div><br></div>" & current_record_body
set body of note current_record_title in folder current_record_type of account "iCloud" to current_record_body
else
log "Note exists"
log (get body of note current_record_title in folder current_record_type of account "iCloud")
set body of note current_record_title in folder current_record_type of account "iCloud" to current_record_body
end if
end tell
--return "first record done"
end if
end repeat
end run
on strip_quotes(value)
if character 1 of value = "\"" then
set value to (characters 2 through (count characters in value) of value) as text
end if
if character (count characters of value) of value = "\"" then
set value to (characters 1 through ((count characters in value) - 1) of value) as text
end if
return value
end strip_quotes
on clean_field_name(field_name)
-- Remove parenthesis-enclosed repetitions
set parenthesis_offset to offset of "(" in field_name
if (parenthesis_offset is greater than 0) and (character (count characters of field_name) of field_name) = ")" then
set field_name to (characters 1 through (parenthesis_offset - 1) of field_name) as text
end if
-- Title Case
set field_name to do shell script "php -r \" echo ucwords(" & quoted form of field_name & ");\""
return field_name
end clean_field_name
on clean_field_value(field_value_raw)
set field_value to strip_quotes(field_value_raw)
-- Is this a date? Convert to ISO 8601
if (offset of "/" in field_value) is greater than 0 then
set AppleScript's text item delimiters to "/"
set data_pieces to every text item of field_value
set AppleScript's text item delimiters to original_text_item_delimiters
if ((count data_pieces) = 3) and ((count field_value) ≥ 6) and ((count field_value) ≤ 10) then
set field_value to "20" & item 3 of data_pieces & "-" & leading_zero(item 2 of data_pieces) & "-" & leading_zero(item 1 of data_pieces)
end if
end if
return field_value
end clean_field_value
on leading_zero(the_number)
if (the_number as number) < 10 then
set the_number to "0" & (the_number as text)
end if
return the_number
end leading_zero
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment