Created
June 6, 2017 15:31
-
-
Save sirtimbly/7d287efd2e61fd3db08039c8ce5e6ad5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Create a new task based on the currently selected message in Airmail | |
-- When Script is run (I suggest FastScripts https://red-sweater.com/fastscripts/) A new line will be added to the taskpaper file of your choice. | |
-- Quite a lot modified to input text into a plain text file like used by taskpaper. Originally based somewhere back in time on Efficient Computing's AppleScript: http://efficientcomputing.commons.gc.cuny.edu/2012/03/17/copy-email-message-in-mail-app-to-evernote-applescript/ | |
tell application "Airmail 2" | |
log "get selected messages" | |
set theSelection to selected messages | |
--loop through all selected messages | |
repeat with theMessage in theSelection | |
--get information from message | |
set theMessageDate to short date string of (get current date) | |
log theMessageDate | |
set file_name to my replace_chars(theMessageDate, "-", "") & ".txt" | |
log file_name | |
set theMessageSender to the sender of the theMessage | |
set theMessageSubject to the subject of the theMessage | |
set theMessageContent to the content of the theMessage | |
set theMessageURL to selectedMessageUrl | |
--import message to text file | |
my WriteLog(" | |
- resolve email from " & (word 1 of theMessageSender) & "_at_ " & (word 2 of theMessageSender) & " [" & theMessageSubject & "](" & selectedMessageUrl & ") | |
", file_name) | |
end repeat | |
end tell | |
on WriteLog(the_text, file_name) | |
-- TO GET THIS TO WORK: you must make a symlink (ln -s) in your home directory to the iCloud NotePlan location on your disk | |
set theTaskFile to "/Users/sirtimbly/noteplan/Documents/Calendar/" & file_name | |
log theTaskFile | |
set this_story to the_text | |
--set this_file to (((path to desktop folder) as text) & "test.txt") | |
set this_file to (POSIX file theTaskFile) | |
my prepend_to_file(this_story, theTaskFile) | |
end WriteLog | |
on prepend_to_file(this_data, target_file) -- (string, file path as string, boolean) | |
try | |
--set the target_file to the target_file as text | |
log target_file | |
do shell script "echo \"" & this_data & " $(cat " & target_file & ")\" > " & target_file | |
--open for access target_file | |
--set fileContents to (read target_file from 0 to eof) | |
--close access target_file | |
--log fileContents | |
--set the open_target_file to ¬ | |
-- open for access file target_file with write permission | |
--log open_target_file | |
--set newInboxData to this_data & return | |
--log newInboxData | |
--set fileContent to searchAndReplace(fileContents, "Inbox:", newInboxData) | |
--write fileContents to the open_target_file starting at 0 | |
--write newInboxData & fileContents to the open_target_file starting at 0 | |
--close access the open_target_file | |
return true | |
on error | |
--try | |
--close access file target_file | |
--end try | |
return false | |
end try | |
end prepend_to_file | |
on replace_chars(this_text, search_string, replacement_string) | |
set AppleScript's text item delimiters to the search_string | |
set the item_list to every text item of this_text | |
set AppleScript's text item delimiters to the replacement_string | |
set this_text to the item_list as string | |
set AppleScript's text item delimiters to "" | |
return this_text | |
end replace_chars |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment