Archives

Tag: AppleScript

  • Automating Blog Posts with AppleScript v2


    I decided that my earlier attempt at automating creation of these TIL posts was still too manual. This one is better!

    What it does:

    1. Creates a markdown file with today’s date and my specified string at the title.
    2. Launches Coda in the foreground and goes to my preset Jekyll blog site (cagrimmett.com)
    3. Opens the file it just created in the front window.
    4. Expands the previous TextExpander snippet I created as a template for these TIL posts.

    The Script

    %filltop%  on textexpander(abbreviation)	 do shell script "cd ~/Projects/cagrimmett-com/_posts/til; touch %snippet:fname%"  tell application "Coda 2" 	activate window 	get site 6 of application "Coda 2" 	tell window 1 		connect to site 6 of application "Coda 2" 			open "Macintosh HD:Users:CAG:Projects:cagrimmett-com:_posts:til:%snippet:fname%" 	end tell end tell tell application "TextExpander" 	expand abbreviation ";tem" end tell  end textexpander

    Here are what the other two snippets fill in:

    fname– I had to make this a snippet instead of putting it in directly because in the filename creation string TextExpander didn’t recognize the date string. If you nest it inside another snippet, it works just fine.

    %Y-%m-%d-%filltext:name=title%.md 

    ;tem

    --- layout: post title:  author: Chuck Grimmett date: %Y-%m-%d category: TIL feature-img: "/img/defaults/%snippet:;random-img%" tags:  -  excerpt: 

    --- ### Today I learned:
  • Dealing with Files in AppleScript and Conditional Counts in Excel


    I took a 10-day break from my TILs and now I’m reinvigorated and back on track. In the past 10 days I spent a lot of time doing things I already knew how to do, but I also worked on a small project related to data visualization to see if I could apply some things I learned over the past few months. The result was my recent blog post on Steph Curry’s stats.

    A new project

    I got a lot of good feedback on Facebook on some more things I can explore (and about how little I understand basketball.) I decided to work on another personal project to apply some of the data science I’ve been learning by reading Joel Grus’s Data Science from Scratch. I decided to find some data (photo dates and locations), extract it (AppleScript), format it (grep FTW), analyze it (forthcoming in Excel and Python), then visualize the insights (Python and/or D3). You can follow my progress on GitHub.


    Dealing with files in AppleScript

    AppleScript natively deals with file paths with colons: Macintosh HD:usr:local:bin: If you get a file path then want to pass it to the shell, you’ll first need to turn it into a POSIX path:

    set a to "Macintosh HD:usr:local:bin:"  set p to POSIX path of a     -- Output: "/usr/local/bin/"

    If you want to create a file in the same folder as a script you are running, you might have to jump back and forth between AppleScript paths and POSIX paths because it is easier to make files by using the command do shell script. Notice the use of quoted form of, which quotes the file path and keeps you clear of pesky errors caused by characters in the path that need to be escaped:

    -- Creating the file in the same folder as this script set scriptPath to POSIX path of ((path to me as text) & "::") do shell script "> " & quoted form of scriptPath & "photo_dates_location.csv"

    Then if you want to get the path to the file you just created and make an it an alias to reference later:

    set filePath to ((path to me as Unicode text) & "::") & "photo_dates_location.csv" set theFile to filePath as alias

    Conditional counting in Excel

    Suppose you have a [spreadsheet full of photos, the date they were taken, and the days of the week they were taken on] (https://github.com/cagrimmett/apple_photos_analysis) and you want to count how many were taken on Monday, Tuesday, etc. Then you’d use the function COUNTIFS(range,argument) to work it out.

    Example: Suppose I have the days of the week in column A and cell H2 contained the word I was looking for, Monday. Then my formula would be:

    =COUNTIFS(A2:A8500,H2) 

    I quickly repeated this for the cells that contained the values for the other days of the week and got instant results:

    Day Count
    Monday 900
    Tuesday 906
    Wednesday 1079
    Thursday 1082
    Friday 1186
    Saturday 1918
    Sunday 1331