Safari Text Encoding (Applescript)

Discussion in 'Mac Programming' started by greenx, Jan 7, 2011.

  1. greenx macrumors newbie

    Joined:
    Jan 7, 2011
    #1
    HI All,

    I wanna write an applescript to change the Text Encoding under "View" menu,every time I open safari.

    since it will automatically be reverted to "Default" every time I quit Safari (very retarded!!)

    But I'm no guru of programming. I don't know how to click a sub-menu..

    This is all what I know:
    activate application "Safari"

    tell application "System Events"

    tell process "Safari"

    click menu item "Text Encoding" of menu 1 of menu bar item "View" of menu bar 1
    delay 1
    {The next step will be clicking the encoding I want, e.g. : Western(IOS Latin 1)....but this is where I need you guys help!!}
    end tell
    end tell


    Thanks a lot everyone!!!
     
  2. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #2
    Doesn't
    Code:
    Safari -> Preferences -> Appearance -> Default Encoding
    allow you to change the default. :confused:

    B
     
  3. greenx thread starter macrumors newbie

    Joined:
    Jan 7, 2011
    #3
    NO that's different

    You can try on any website...

    change the encode under view menu, go to the website, then quit safari, go the the website again...encoding reverts to default...

    This is the bullcrap of mac...

    and I;m trying to fix it myself
     
  4. jiminaus, Jan 8, 2011
    Last edited: Jan 8, 2011

    jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #4
    Actually sounds to me a problem that the website is not correctly (if at all) declaring the text encoding being used.

    BTW Have you tried other browsers. All the big browsers (except IE of course) are available for the Mac.
     
  5. greenx thread starter macrumors newbie

    Joined:
    Jan 7, 2011
    #5
    Thanks for you guys response..

    However,

    1)

    I want to modify encoding NOT because I can't read some webpage..
    but I don;t want to read site in certain language...I wanna filter out these pages..

    So, none of the websites biz...lol..

    2)

    I'm sticking to Safari, cuz' I hate Microsoft & stupid UI of firefox..So switch browser won't work here either...

    All I'm asking is to make an Applescript to change text encoding every time I open the browser...(Refer to my first post)

    Anyone??
    PS:
    For those who think change encoding under preference menu read this thread first : http://discussions.apple.com/thread.jspa?messageID=12563666
     
  6. balamw, Jan 8, 2011
    Last edited: Jan 8, 2011

    balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #6
    This is what we are saying. Default is what you tell it to be, but it is overridden by code from the website. Maybe the website in question has a preference/cookie of its own to change the language/encoding.

    Can you tell us what website it is?

    EDIT: Here's a link to a tutorial on scripting menu items if you absolutely have to "go there". http://www.macosxautomation.com/applescript/uiscripting/index.html

    B
     
  7. greenx thread starter macrumors newbie

    Joined:
    Jan 7, 2011
    #7
    Oh I see what you mean, sorry for the misunderstanding..

    but it's not about any site..

    even i change the encoding on google.com

    next time i go to the page the encoding will still revert..

    how frustrating is that....
     
  8. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #8
    Why is that a problem. It reverts to the Default value you set in Preferences. (You define what Default is). I agree that it would be nice if it indicated what that was instead of "Default".

    B
     
  9. greenx thread starter macrumors newbie

    Joined:
    Jan 7, 2011
    #9
    Really, is that the way it is supposed to work?
    Now I know what this twist is all about. Let me restate my case with more detail:
    Let's take http://www.amazon.co.jp/ for example

    I open safari preference, change the default encoding to, say Cyrillic, which obviously can't display japanese correctly.

    Then I restart safari, open Amazon(JP), it reads japanese perfectly. Looks like Safari will detect encoding itself or somepin. i don't know if it just happens to me, but according to the post at the bottom of this , I seems this is how it's supposed to work.

    Which means It does revert to default but NOT the default I set under preference but the application or system default which is, maybe uni-8 or Latin-1, I'm not sure, but sure it reads any language pretty smooth...

    So I open a new page, goes to Amazon(JP), change the encoding to Cyr- under the view menu. The page won't display proper as I expected. But next time I open safari the encoding under View menu will revert, that's what Im saying. This explains why I need the applescript.....

    SOS!!!!
     
  10. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #10
    No it doesn't, because you are obviously not trying to view amazon.co.jp in Cyrillic.

    The source for amazon.co.jp includes many references to
    Code:
    encoding=UTF8
    So when you go to amazon.co.jp that is what is respected.

    This is why we were blaming the website you are trying to visit, obviously not amazon.

    B
     
  11. greenx thread starter macrumors newbie

    Joined:
    Jan 7, 2011
  12. greenx thread starter macrumors newbie

    Joined:
    Jan 7, 2011
    #12
    Anyway, thanks a lot "balamw"..appreciate your help.

    In the meantime, can anyone finish that applescript for me...even with the link(no examples), I'm still having problems get it done..

    sigh....
     
  13. ulbador macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #13

    Here you go... This is the EXTREME longhand version of this, but it works.. Change the encoding in the middle buried line to the one you want:
    Code:
    tell application "Safari"
    	activate
    end tell
    
    tell application "System Events"
    	tell process "Safari"
    		tell menu bar 1
    			tell menu bar item "View"
    				tell menu "View"
    					tell menu item "Text Encoding"
    						tell menu "Text Encoding"
    							click menu item "Greek (Windows)"
    						end tell
    					end tell
    				end tell
    			end tell
    		end tell
    	end tell
    end tell
    
     
  14. balamw, Jan 9, 2011
    Last edited: Jan 9, 2011

    balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #14
    Thanks ulbador.

    What I am missing is why that is desirable? If you set the default to "Cyrillic (Mac OS)" instead of "Western (ISO Latin 1)" in "Preferences -> Appearance" that will cause text that does not have a defined encoding to appear in your selected Default encoding.

    In the case of Amazon.co.jp, most text is defined by the site to be in UTF-8 and should properly be represented as such. No?

    EDIT: The blame still lies with whatever website you are using that isn't sending the proper charset with their pages.

    If you
    Code:
     telnet amazon.co.jp 80
    GET /
    
    You will find:
    Code:
     <meta http-equiv="content-type" content="text/html; [B]charset=Shift_JIS[/B]" />
    
    near the top of the reply.

    (Similarly macrumors specifies:
    Code:
    <meta http-equiv="Content-Type" content="text/html; [B]charset=utf-8[/B]" />
    
    )

    Safari sees that and respects the website's wishes, but also gives you the option to override that after the fact if the website is poorly coded. (Which amazon.co.jp and macrumors are not).

    What jiminaus and I are saying is that ultimately the problem lies with the website you are using sending the wrong encoding from their site.

    If the website doesn't specify an encoding, changing the default to "Cyrillic (Mac OS)" should have the same effect as your AppleScript [See attached pic]


    B
     

    Attached Files:

  15. greenx thread starter macrumors newbie

    Joined:
    Jan 7, 2011
    #15
    @Bal-: ooooooh

    So if a site is well programmed...no matter what you do, ya still can't change its encoding controlled by default setting.?
     
  16. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #16
    Why would you do that?

    If the site is correctly providing the encoding, then you telling the browser to use any other encoding is wrong.

    If the site is not providing any encoding, then the default will be used. Since there is a setting for this in a Preferences pane, why wouldn't you set it there? Why would you put something you didn't want in the Preferences pane, which then forces you to choose the default you want from the Text Encoding menu?

    If the site is providing the wrong encoding, then the site is wrong. The default encoding may also be wrong, but whether it's right or wrong will vary depending on the website. So you'd have to know what the website is, know that its encoding is wrong, and then manually choose the right encoding. That can't be automated because it takes a human to recognize the website is wrong, and figure out what the right encoding is.
     
  17. greenx thread starter macrumors newbie

    Joined:
    Jan 7, 2011
    #17
    That means I don't wanna read some characters.

    That's what I'm asking: if there is a way to overwrite encoding to preference default, regardless what the website programmed
     
  18. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #18
    This isn't how Safari rolls. It adheres to web standards and actually wants you to read the text on websites.

    As jiminaus said earlier in the thread, other browsers may behave differently. Try one. Or use other approaches to eliminate the text you object to for some reason and extract only what you want.

    B
     
  19. greenx thread starter macrumors newbie

    Joined:
    Jan 7, 2011

Share This Page