Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
qiaokang0 opened this issue Jun 15, 2020 · 1 comment
Open

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

qiaokang0 opened this issue Jun 15, 2020 · 1 comment

Comments

@qiaokang0
Copy link

qiaokang0 commented Jun 15, 2020

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
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants