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 7:26 AM in response to Wagringo

Bizarre so the dayone program is not in /usr/local/bin (the usr directory at the root of the drive) but in usr/local/bin? And where is usr/local/bin?


Also having the command in a try block with no error part is a problem.


Actualy loking at it all your try blocks have no error part. Why? if a command fails the error is eaten and the script continues wiht no indication that something happened.

Apr 23, 2013 7:34 AM in response to Frank Caggiano

Sorry Frank, I missed the leading '/' there! The script has the correct path, and I have edited this block:


try
          do shell script "/usr/local/bin/dayone new < " & out_file
on error
          display dialog "Couldnt write to Day One"
end try


This hasn't had any effect that I can see though...


Also:


I should point out that there is an entry being created in the journal, it is just blank

Apr 23, 2013 7:54 AM in response to Frank Caggiano

Fair enough! I just added on errors in...


I am suspicious of the ' · ' characters. Do you think they are a likely source of the problem?


Here is the script now:

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 & "
"

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

set out_file_ref to open for access POSIX file out_file with write permission
try
  write quoted form of text_out to out_file_ref
on error
          display dialog "Couldnt write to tmp file"
end try
close access out_file_ref

try
          do shell script "/usr/local/bin/dayone new < " & out_file
on error
          display dialog "shell couldnt write to Day One"
end try

delay 0.3

try
  do shell script "rm " & out_file
on error
          display dialog "shell couldnt remove tmp file"
end try

Apr 23, 2013 7:57 AM in response to Wagringo

Most likely this is a problem with html entities - specially encoded characters used in html strings - not text encoding per se. html entities don't usually work well in normal text contexts. Unfortunately applescript doesn't have a built-in method for converting html entities to their text equivalents. Maybe there's a perl script?


I'd have to look at the actual webpage before I could say more.

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.