Hammerspoon / hammerspoon

Staggeringly powerful macOS desktop automation with Lua
http://www.hammerspoon.org
MIT License
12k stars 581 forks source link

hs.urlevent.openURL() doesn't work with Chinese #2398

Open qiaokang0 opened 4 years ago

qiaokang0 commented 4 years ago

I want create a workflow to search the text i input in dialog, since the url parameter for key words to search in google(if the keyword is "中国") is "https://www.google.com/search?q=中国" so i run this function in lua:

hs.hotkey.bind({"control","alt","command","shift"}, "1", function()
    hs.application.launchOrFocus("Hammerspoon.app")
    my_button,my_text=hs.dialog.textPrompt("Google", "contentment right now", "", "yep", "nope")
    search_engine="https://www.google.com/search?q="
    my_text=search_engine..my_text
    hs.urlevent.openURL(my_text)
end)

it works fine if i insert English,but no lucky with Chinese.

now i come up with a alternative solution :

hs.hotkey.bind({"control","alt","command","shift"}, "2", function()
    hs.application.launchOrFocus("Hammerspoon.app")
    my_button,my_text=hs.dialog.textPrompt("百度", "自律才是自由", "", "yep", "nope")
    search_engine="https://www.baidu.com/s?wd="
    my_text=search_engine..my_text
    my_command="open "..my_text
    os.execute(my_command)
    --hs.urlevent.openURL(my_text)
end)

this works fine in both English and Chinese

asmagill commented 4 years ago

I can confirm this behavior.

I won't be able to spend time investigating this until next week at the earliest, so here are my initial notes in case someone wants to take a look at it sooner:

hs.urlevent.openURL, after going through a wrapper function that I have confirmed does not damage the input in any way, resolves to hs.urlevent.openURLWithBundle. The relevant code is at https://github.com/Hammerspoon/hammerspoon/blob/master/extensions/urlevent/internal.m#L301-L308

    NSURL *url = [NSURL URLWithString:[skin toNSObjectAtIndex:1]];

    if (url) {
        result = [[NSWorkspace sharedWorkspace] openURLs:[NSArray arrayWithObject:url]
                                 withAppBundleIdentifier:[NSString stringWithUTF8String:lua_tostring(L, 2)]
                                                 options:NSWorkspaceLaunchDefault additionalEventParamDescriptor:nil
                                       launchIdentifiers:nil];
    }

I don't know yet if NSURL is having issues with the characters being unencoded (though the HTTP spec doesn't require them to be since they are valid UTF-8 and don't match any of the special codes for GET URLs) or with NSWorkspace's openURLs:withAppBundleIdentifier:options:additionalEventParamDescriptor:launchIdentifiers:. It should be noted that this method for NSWorkspace is deprecated in 10.15, but since Hammerspoon tries to support at least the latest three versions of macOS, even if the best solution involves using the newer NSWorkspace methods, code should be implemented to do so in a way that falls back (and ideally also fixes this for that implementation) for earlier macOS versions.

wuyou36163 commented 5 months ago

It helps. Thank you for offering this method!