Jump to content

AppleScript for Text Formatting


Recommended Posts

Hello. First time posting.

So I have been teaching myself web design & development, and using Evernote to keep notes on various languages and frameworks. These notes have, of course, code snippets, mostly that I copy in or type out by hand (for example, as I watch a video on Code School). These are interspersed with my explanations and comments. So I am typing my note in the default font, Helvetica Neue, and then going back through and changing the code snippets to Courier New. I know I can go and put my snippets into some code formatter web app and paste them back in, but for my purposes all I really want is for snippets to be a different font. And I want to avoid the hassle of selecting the snippet and going through the font dropdown every time.

I was wondering if there was a way to do this. I have been fiddling with AppleScript, trying to make a Service that I could then bind to a keyboard shortcut, but Evernote doesn't seem to allow access to the actual content of notes from AppleScript. Right now I'm trying to save the note HTML content to a variable and then add a font tag to lines that start with a certain character, but I was just introduced to AppleScript today so I don't really know what I'm doing.

Here's the lame attempt I have so far:

property noteHtml : ""
property addOn : "<br><br><hr><br><br>"

tell application "Evernote"
	activate
	set note1 to selection
	set noteHtml to (HTML content of note1)
	set addString to "<br>"
	if {noteHtml starts with "<p>"} then
		set editHtml to ?
		set noteHtml to editHtml
	end if
end tell

Can anyone offer any tips or a solution? Thanks.

Link to comment

Well, after some more digging in the forums, I have found a workaround (only works for OSX). In System Preferences -> Keyboard -> Shortcuts -> App Shortcuts, you just make a new shortcut with a title = font name, and set the key combo. Good enough for me, but please if you have something better don't hesitate to post!

Link to comment
  • Level 5*
20 hours ago, grumpyclam said:

Well, after some more digging in the forums, I have found a workaround (only works for OSX). In System Preferences -> Keyboard -> Shortcuts -> App Shortcuts, you just make a new shortcut with a title = font name, and set the key combo. Good enough for me, but please if you have something better don't hesitate to post!

That sounds like a better solution than applescript to mess with the html code.
You could also look at the keyboard macro-shortcut apps (example Keyboard Maestro)
I don't use them but I understand they will allow you to set up a series of keystrokes to simplify the font assignment.

Link to comment

By converting the paragraph to string you are inherently stripping all the formatting data.Instead, you should reference the data as text.Text retains formatting and style data and will do what you want. Replace every instance of 'as string' in your script with 'as text' and you should be set.

 

Universal Access System Preference pane.

 

set theReplacements to 

    {"0mg ", "0 mg "}, ¬

    {"5mg ", "5 mg "}, ¬

    {"  ", " "} ¬

        }

 

tell application "Pages" to activate

tell application "System Events" to tell process "Pages"

    keystroke "f" using command down

    repeat until exists window "Find & Replace"

    end repeat

    tell window "Find & Replace"

        click radio button "Simple" of tab group 1

        repeat with thisReplacement in theReplacements

            set {X, Y} to thisReplacement

            keystroke X

            keystroke tab

            keystroke Y

            keystroke tab

            click button "Replace All" of tab group 1

        end repeat

        click button 1

    end tell

end tell

 

The script has been tested under OS X 10.8.1.

 

 

 

 

hadoop training in chennai | informatica training in chennai

Edited by richardsonkane75
modify
Link to comment
  • Level 5*
23 hours ago, richardsonkane75 said:

tell application "Pages" to activate

Thanks for sharing your script.

Since your script is working with the Mac app Pages, how do you suggest it be used with Evernote?
Remember that EN Mac does NOT have a "Find & Replace" menu/action/command.

Link to comment
  • 1 year later...

FWIW, I was looking into using Applescript to format single lines of text  for title headers in notes. I just got fed up with digging around in Font dialogs when all I wanted was for this line to be H2, or something. It uses System events to select and grab the current line into the clpboard then calls the textutil standalone to do the formatting, pasting the result back into the note. It's kludgy but functional. The most significant annoyance is that it requires 2 1 second sleeps. I see no way around that.

For myself I have it stored as a set of Keyboard Maestro Macros for varying sizes, triggered by text strings #, ##, ###, ###, etc as per Markdown - I have shortcut keys coming out my ears and this is easy to remember.

 

delay 1 -- necessary
tell application "System Events"

    keystroke (ASCII character 28) using {command down}
    keystroke (ASCII character 29) using {command down, shift down}

    keystroke "c" using {command down}

end tell

delay 1 -- necessary

-- set fontsize to 28
do shell script "pbpaste | textutil -stdin -stdout -convert rtf -fontsize 28 | pbcopy"
tell application "System Events" to keystroke "v" using command down

Enjoy

Link to comment
  • Level 5*
3 hours ago, kimaldis said:

I just got fed up with digging around in Font dialogs

fwiw   On the Macs you can assign a keyboard shortcut to any menu item (including fonts and sizes).  The style update could be implemented without calling TextUtil

Link to comment
  • Level 5*
3 hours ago, DTLow said:

On the Macs you can assign a keyboard shortcut to any menu item (including fonts and sizes).  The style update could be implemented without calling TextUtil

I'd be interested in seeing how you would do this.  Neither the Font popup nor the Font Size popup are in the Evernote menu.  How would you assign a shortcut to a specific font and size?

Link to comment
  • Level 5*
43 minutes ago, JMichaelTX said:

How would you assign a shortcut to a specific font and size?

These are the font/size controls being used 593eee2822344_ScreenShot2017-06-12at12_38_55.png.83cbefd91a59117205dec511e0fc9501.png

Shortcuts assigned in system preferences                          And they are functional

593eeb63279e4_ScreenShot2017-06-12at12_21_14.png.452f5701b44279e9420f0713eaef89e5.png               593eef15dabd3_ScreenShot2017-06-12at12_43_29.png.3dea03d2b1102c54e94ac1173ac0332b.png   593eea29236b7_ScreenShot2017-06-12at12_22_42.png.9f50e3a3a7820aeef82ba136afb55268.png

 

 

The AppleScript code is
    tell application "System Events" to keystroke "h" using {command down, option down, control down}
    tell
application "System Events" to keystroke "1" using {command down, option down, control down}

  • Like 1
Link to comment
  • Level 5*
On 3/7/2016 at 3:56 AM, grumpyclam said:

So I am typing my note in the default font, Helvetica Neue, and then going back through and changing the code snippets to Courier New.
. . .
but for my purposes all I really want is for snippets to be a different font. And I want to avoid the hassle of selecting the snippet and going through the font dropdown every time.

I was wondering if there was a way to do this. I have been fiddling with AppleScript . . .

You will still have to select the code snippet in Evernote, but you can use an AppleScript to automatically apply a font and size to the snippet.
See this script I have just posted on a GitHub Gist:  Set Evernote Mac Font and Text Size using AppleScript 

If you store the script in the standard Scripts folder, you can access it  from the Apple menu bar.  And/or you can use a tool like FastScripts (recommended) to assign a shortcut to the script.

If you have any comments, questions, or issues about this script, please post them here so I will be notified, and all can see.

Link to comment
17 hours ago, DTLow said:

fwiw   On the Macs you can assign a keyboard shortcut to any menu item (including fonts and sizes).  The style update could be implemented without calling TextUtil

The script selects the current line for you; that you can't do from a menu.

Link to comment
  • Level 5*
3 hours ago, kimaldis said:

The script selects the current line for you; that you can't do from a menu.

For selecting a line; the command-arrow keystrokes work well, although I'm more inclined to use mouse clicks
I see no need for a menu item

Link to comment
2 hours ago, DTLow said:

For selecting a line; the command-arrow keystrokes work well, although I'm more inclined to use mouse clicks

But the script saves you that step. It's quicker and why do it if you don't need to.

Link to comment
  • Level 5*
2 hours ago, kimaldis said:

But the script saves you that step. It's quicker and why do it if you don't need to.

I have no issue with the script selecting the line; it's an elegant solution
My origional response related to calling TextUtil to apply the font/size style; instead,  keystrokes can be used

  • Like 1
Link to comment
  • 2 years later...

OK, here's my take on this:

* System prefs, shortcuts to assign shortcuts to font size (4 - 14,18,24,36) and font (1, Arial) - thanks @DTLow for that, I assumed it only applied to top menubar items

* BetterTouchtool to put buttons in the touch bar for Headings 1,2,3 and normal which calll applescripts when pressed. Heading scripts select the line then send shortcut keystrokes to apply font and size to the line from the menu. Normal  selects the paragraph rather than a line.

Better touch tool and the touch bar because I can't be. bothered to remember the shortcuts, which can be obscure because looking for ones that aren't taken. ( I used <ctrl>1,2,3,4,5 )

 

tell application "System Events"
	
	key code 123 using {command down}
	key code 124 using {command down, shift down}
	
	keystroke "1" using {control down} -- H1 (36)
	delay 0.1
	keystroke "5" using {control down} -- Arial
		
end tell

 

tell application "System Events"
	
	key code 126 using {option down}				-- paragraph select
	key code 125 using {option down, shift down}
	
	keystroke "4" using {control down}				-- size 14
	delay 0.1
	keystroke "5" using {control down}				- Arial
	
		
end tell

I've struggled for ever to get something close to OneNote's rather slick text formatting workflow and, for me, this is about as good as it needs to be. I can literally place the cursor, hit a key and the line or paragraph is transformed instantly

The advantage of BetterTouchTool wrapping it is a) no shortcut to remember and b) you get to combine both font and font size changes in one hit. If you don't have a tuch bar, assign the scripts to key presses or sequences.

I didn't really do much, it's just a gathering of what's gone before here, so thanks to those that went before.

Next battle, proper tag hierarchies and infinite notebook nesting depth. Forward!!

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...