Create a gist now

Instantly share code, notes, and snippets.

Embed
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
set theFile to (choose file with prompt "Select a file to read:" of type {"txt"})
open for access theFile
set thePath to the POSIX path of theFile
set fileContents to (do shell script "cat " & quoted form of thePath)
set Mytitle to paragraph 1 of fileContents
set Mytext to paragraph 2 of fileContents
set Myurl to paragraph 3 of fileContents
close access theFile
--get the name of the old folder
set foldername to Mytitle
set find to "–"
set replace to "--"
set someText to foldername
set foldername to my replaceText(find, replace, someText)
set find to "|"
set replace to "--"
set someText to foldername
set foldername to my replaceText(find, replace, someText)
set find to " "
set replace to "-"
set someText to foldername
set foldername to my replaceText(find, replace, someText)
set find to "&"
set replace to ""
set someText to foldername
set foldername to my replaceText(find, replace, someText)
set find to "@"
set replace to ""
set someText to foldername
set foldername to my replaceText(find, replace, someText)
set find to ":"
set replace to ""
set someText to foldername
set foldername to my replaceText(find, replace, someText)
set foldername to do shell script "echo " & quoted form of (foldername) & " | tr A-Z a-z"
--Find the old file
set myPath to POSIX path of ("/Applications/MAMP/htdocs/grav-admin/user/pages/04.reviews" as text) & "/" & foldername
set oldFile to myPath & "/item.md"
set oldContent to (do shell script "cat " & quoted form of oldFile)
--Read the old file and lop off the end
set contentList to paragraphs of oldContent
set itemNo to count of contentList
set itemNo to (itemNo - 1)
set oldNote to item itemNo of contentList
--Add the new highlight
set oldContent to my replaceText(oldNote, "", oldContent)
set oldContent to oldContent & "
<p class=\"highlight\">" & Mytext & "</p>"
--Change wording of Notes if needed
if oldNote contains "<p>Note from" then
set oldNote to my replaceText("Note", "Notes", oldNote)
end if
set oldContent to oldContent & "
" & oldNote
--write content out to file
do shell script "echo " & quoted form of oldContent & " > " & quoted form of oldFile
display dialog oldContent
on replaceText(find, replace, someText)
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to find
set someText to text items of someText
set text item delimiters of AppleScript to replace
set someText to "" & someText
set text item delimiters of AppleScript to prevTIDs
return someText
end replaceText
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment