Last active
October 28, 2016 23:24
-
-
Save patrickwelker/6067571 to your computer and use it in GitHub Desktop.
This file contains 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
----------------------------------------------------- | |
-- Name: Edit Last Modified Note and Preview in Marked | |
-- Author: Patrick Welker <http://rocketink.net> | |
-- Version: 1.1 (October 24, 2014) | |
-- Credits: Uses ViewInMarked by Robin Trew to align editor and Marked windows: <http://git.io/sLxcgg> | |
-- For: bloggers, writers and Marked users (<http://markedapp.com>) | |
----------------------------------------------------- | |
-- Know problems: | |
-- * Sometimes, when the editor is closed it first tries top open the default "open" dialog. | |
-- * The script handles nvALT differently from all editors. It doesn't resize nvALT's window since most users | |
-- like to keep their nvALT at a specific size. On the downside: if nvALT gets chosen along with Marked | |
-- preview, Marked takes about 7 seconds to align itself. I don't know why. | |
-- * As explained here (<http://macscripter.net/viewtopic.php?id=26334>) you need to setup tell blocks | |
-- around dialogs and lists to make the script work fine outside of the AppleScript Editor and avoid the | |
-- "-1713 error". I wasn't capable of figuring out where the tell blocks should start and end without getting | |
-- errors on the handlers. So to run the script needs it needs to be exported as an app or started with an | |
-- runner script. --- It would be great if someone can "help me understand" where the problem is so that I | |
-- can fix it. | |
----------------------------------------------------- | |
---- User Configuration | |
-- Get user data | |
property notesFolder : "/Users/you/Dropbox/Notes/" -- include leading and trailing slash | |
property editorList : {"FoldingText", "Sublime Text 2", "nvALT", "Byword", "MultiMarkdown Composer", "Mou"} | |
-- Marked Setup | |
property alwaysMarked : false -- Don't prompt, *always* open Marked | |
--set alwaysMarked to true -- Don't prompt, *always* open Marked | |
property openMarked : true -- Prompt to open a preview in Marked | |
property markedDialogDefault : true -- Set to false for "no" button to be highlighted | |
property alignWindows : true -- Set this to false to disable the window positioning at the end of the script | |
property alignWindowsDefault : true -- Set this to false to position the preview on the left side | |
-- Search (optional) | |
property xNotes : 15 -- set how many recently modified notes should be displayed | |
property searchNotes : "*.{md,txt}" -- specify what notes to display, c.f. *draft*.{md,txt,taskpaper,mmd} | |
-- nvALT Setup (optional) | |
property nvPosition : false -- Set this to false if your nvALT is usually on the right side of the screen | |
----------------------------------------------------- | |
-- Set handlers | |
on cancelHandler(i) | |
if i is false then | |
beep 1 | |
error number -128 | |
end if | |
end cancelHandler | |
on markedHandler(filePath) | |
tell application id "com.brettterpstra.marked2" | |
activate | |
open filePath | |
end tell | |
end markedHandler | |
on editorHandler(selectedEditor, filePath) | |
tell application selectedEditor | |
activate | |
open filePath | |
end tell | |
end editorHandler | |
on alignHandler(alignWindows, selectedEditor) | |
-- Align the opened windows | |
if alignWindows then | |
set lngWidth to word 3 of (do shell script "defaults read /Library/Preferences/com.apple.windowserver | grep -w Width") | |
set lngHeight to word 3 of (do shell script "defaults read /Library/Preferences/com.apple.windowserver | grep -w Height") | |
set lngHalf to lngWidth / 2 | |
set lngHeight to lngHeight - 22 | |
tell application id "sevs" | |
if alignWindowsDefault then | |
if selectedEditor ≠ "nvALT" then | |
tell process "Marked 2" to tell window 1 to set {position, size} to {{lngHalf, 22}, {lngHalf, lngHeight}} | |
tell process selectedEditor to tell window 1 to set {position, size} to {{0, 22}, {lngHalf, lngHeight}} | |
else if selectedEditor = "nvALT" and nvPosition then | |
tell process "Marked 2" to tell window 1 to set {position, size} to {{0, 22}, {lngHalf, lngHeight}} | |
else | |
tell process "Marked 2" to tell window 1 to set {position, size} to {{lngHalf, 22}, {lngHalf, lngHeight}} | |
end if | |
else | |
if selectedEditor ≠ "nvALT" then | |
tell process selectedEditor to tell window 1 to set {position, size} to {{lngHalf, 22}, {lngHalf, lngHeight}} | |
tell process "Marked 2" to tell window 1 to set {position, size} to {{0, 22}, {lngHalf, lngHeight}} | |
else if selectedEditor = "nvALT" and nvPosition then | |
tell process "Marked 2" to tell window 1 to set {position, size} to {{0, 22}, {lngHalf, lngHeight}} | |
else | |
tell process "Marked 2" to tell window 1 to set {position, size} to {{lngHalf, 22}, {lngHalf, lngHeight}} | |
end if | |
end if | |
end tell | |
end if | |
end alignHandler | |
-- Get Last Modified | |
set getList to "cd " & notesFolder & "; ls -at " & searchNotes & " | head -n" & xNotes | |
set lastModified to do shell script getList | |
set {input, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return} | |
set notesList to every text item of lastModified | |
set AppleScript's text item delimiters to input | |
set selectedNote to item 1 of (choose from list the notesList with title "The " & xNotes & " last modified notes" with prompt "Choose note to edit:") | |
cancelHandler(selectedNote) | |
-- Get Path and Editor | |
set filePath to POSIX file (notesFolder & selectedNote) as alias | |
set defaultApp to item 1 of editorList -- Alternatively specify a default like this {"FoldingText"} | |
set selectedEditor to item 1 of (choose from list the editorList with title "Editor List" default items defaultApp) | |
cancelHandler(selectedEditor) | |
-- Marked preview dialog prompt | |
if alwaysMarked then -- Skip prompt completely & preview directly | |
markedHandler(filePath) | |
editorHandler(selectedEditor, filePath) | |
alignHandler(alignWindows, selectedEditor) | |
else | |
if markedDialogDefault ≠ true then | |
set buttonNumber to 1 | |
else | |
set buttonNumber to 2 | |
end if | |
if openMarked then | |
set openMarkedTitle to "Open in Marked, too?" | |
try | |
set getButton to display dialog openMarkedTitle buttons {"No", "Yes"} default button buttonNumber cancel button 1 with title "Preview" with icon path to resource "ApplicationIcon.icns" in bundle (path to application "Marked 2") | |
markedHandler(filePath) | |
editorHandler(selectedEditor, filePath) | |
alignHandler(alignWindows, selectedEditor) | |
on error -- Don't open Marked | |
editorHandler(selectedEditor, filePath) | |
end try | |
else -- No preview, just open the editor | |
editorHandler(selectedEditor, filePath) | |
end if | |
end if |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated to work with Marked 2.