Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Am I having encoding trouble with this script?

G'day,


I have a simple applescript, intended to get the title and url from current webpage in order to log to a text-based journaling app called Day One:



set out_file to "/Users/home/.tmp_dayone"  

tell application "Safari"           
     set current_tab to current tab of front window           
     set tab_name to name of current_tab as Unicode text           
     set get_url to URL of current_tab as Unicode text 
end tell  


set text_out to "Read on the Web:  " & tab_name & " " & get_url & " " as Unicode text
set out_file_ref to open for access POSIX file out_file with write permission 
try   
     write text_out to out_file_ref 
end try 
close access out_file_ref  


try           
     do shell script "usr/local/bin/dayone new < " & out_file 
end try  


delay 0.3  

try   
     do shell script "rm " & out_file 
end try



The script is obviously not very robust but mostly works. However...


...At this page http://www.ppowgallery.com/selected_work.php?artist=25 the

name of current_tab

returns this title:

"P·P·O·W | The Estate of Martin Wong | Selected Text"


Saving this to a text file and asking a shell subprocess to write a new DayOne entry from the .txt, only creates a blank entry in the journal. And I can't figure out why!


If I use less at the terminal I can see that the ' · ' character is written as '<E1>' in the .txt. Is this what is causing the problem? If I enter

file -I .tmp_dayone

at the terminal the response says the file is ISO-8859-1 encoded. I don't know if any of this is relevant; I am an amateur at all of this stuff.

Why wouldn't applescript be writing to a file as 'UTF-8" ?


Cheers!

MacBook Pro, OS X Mountain Lion (10.8.3)

Posted on Apr 23, 2013 6:15 AM

Reply
20 replies

Apr 23, 2013 8:11 AM in response to Wagringo

Substituting a space for that char so that the line reads P P O W doing a file on .tmp_dayone says it is ascii


so od -c

LIBMACLAPcaggiano:~ frank$ od -c .tmp_dayone

0000000 R e a d o n t h e W e b :

0000020 P P O W | T h e


(spaces in place of the special char gives


LIBMACLAPcaggiano:~ frank$ file .tmp_dayone

.tmp_dayone: ASCII English text, with no line terminators

LIBMACLAPcaggiano:~ frank$


So now it's just a question of figuring out how to get those switched in the script


The other question that needs to be answered is what is the dayone program doing and what is it written in?

Apr 23, 2013 8:54 AM in response to Wagringo

For most western users, AppleScript writes files in MacRoman. To write in UTF-8, you need to specify that in the write command, for example:



writetext_outtoout_file_refas «classutf8»


The term "as Unicode text" is not needed for your strings since strings in AppleScript are already Unicode, but if you happen to use the term in the write command, it will output UTF-16 (aint AppleScript grand?).


Also, is there a particular reason for writing to an invisible temp file, just to pass it on to your app, or is that also because of encoding issues?

Apr 23, 2013 10:07 AM in response to red_menace

That definitly chages it so that the .tmp_dayone file is reported as UTF8 rather then ISO8859, nice.


But the real question is if the dayone program that is taking this in can deal with this any better then it dealt with the orignal.


There is a dayone app which sounds like what the OP is using in the MAS but it is $10 and of course given the new world of MAS no trial version.

Apr 23, 2013 10:49 AM in response to Wagringo

Hello


Or simply -


tell application "Safari"
    tell window 1's current tab
        set {n, u} to {name, URL}
    end tell
end tell
set t to "Read on the Web:  " & n & " " & u
do shell script "/bin/echo -n " & t's quoted form & " | /usr/local/bin/dayone new"


The 'do shell script' passes the command to shell using UTF-8, hence the t's value which is UTF-16BE is converted to UTF-8 before being passed to shell in the above code. Here I assume the said 'dayone' programme expects UTF-8 text from standard input.


cf.

http://developer.apple.com/library/mac/technotes/tn2065/_index.html


Regards,

H

Apr 23, 2013 4:23 PM in response to red_menace

The main reason I am writing to .tmp_dayone is to try and discover where things go wrong - I'm a learner and am struggling with these finer points I keep butting up against!

Your solution works for me!

Thanks a lot



As a last word:


I was supposed to give the 10 points to red_menace. Dang! (How does one change that?)


I am now aware of

od
thanks to Frank. Cheers Frank!


Hiroto's modification works perfectly, and the tech note linked to is very useful. But I think I learned more making the mistakes with an interim file. Cheers Hiroto!


Many thanks to you all spending some time on my problem. I am grateful!

Am I having encoding trouble with this script?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.