{"id":973141,"date":"2024-12-27T05:54:28","date_gmt":"2024-12-26T21:54:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/973141.html"},"modified":"2024-12-27T05:54:30","modified_gmt":"2024-12-26T21:54:30","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0mac%e9%80%9a%e7%9f%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/973141.html","title":{"rendered":"python\u5982\u4f55\u5b9e\u73b0mac\u901a\u77e5"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24195451\/cf878337-0394-4ffd-b64c-8190a9cfe867.webp\" alt=\"python\u5982\u4f55\u5b9e\u73b0mac\u901a\u77e5\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u5b9e\u73b0Mac\u901a\u77e5\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u201cpync\u201d\u5e93\u3001AppleScript\u3001\u6216\u201cterminal-notifier\u201d\u5de5\u5177\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528\u201cpync\u201d\u5e93\u662f\u4e00\u79cd\u7b80\u5355\u6709\u6548\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u662f\u4e13\u95e8\u7528\u4e8e\u5728Mac\u4e0a\u53d1\u9001\u901a\u77e5\u7684Python\u5e93\uff0c\u975e\u5e38\u6613\u4e8e\u5b89\u88c5\u548c\u4f7f\u7528\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u201cpync\u201d\u5e93\u6765\u5b9e\u73b0Mac\u901a\u77e5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u201cPYNC\u201d\u5e93<\/p>\n<\/p>\n<p><p>\u201cpync\u201d\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684Python\u5e93\uff0c\u4e13\u95e8\u7528\u4e8e\u5728Mac OS\u4e0a\u53d1\u9001\u901a\u77e5\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5pync\u5e93<\/strong><\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5pync\u5e93\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pync<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528pync\u53d1\u9001\u901a\u77e5<\/strong><\/li>\n<\/ol>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u4f7f\u7528pync\u6765\u53d1\u9001\u901a\u77e5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pync import Notifier<\/p>\n<p>Notifier.notify(&#39;Hello, this is a notification!&#39;, title=&#39;Notification Title&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u201cNotifier.notify\u201d\u65b9\u6cd5\u7528\u4e8e\u53d1\u9001\u901a\u77e5\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u901a\u77e5\u5185\u5bb9\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u901a\u77e5\u6807\u9898\u3002\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574\u8fd9\u4e9b\u53c2\u6570\u4ee5\u5b9a\u5236\u901a\u77e5\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u9ad8\u7ea7\u7528\u6cd5<\/strong><\/li>\n<\/ol>\n<p><p>pync\u5e93\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u7528\u6cd5\uff0c\u6bd4\u5982\u8bbe\u7f6e\u901a\u77e5\u58f0\u97f3\u3001\u6253\u5f00URL\u6216\u81ea\u5b9a\u4e49\u56fe\u6807\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbe\u7f6e\u901a\u77e5\u58f0\u97f3<\/p>\n<p>Notifier.notify(&#39;Hello with sound!&#39;, title=&#39;Sound Notification&#39;, sound=&#39;Ping&#39;)<\/p>\n<h2><strong>\u70b9\u51fb\u901a\u77e5\u65f6\u6253\u5f00URL<\/strong><\/h2>\n<p>Notifier.notify(&#39;Click to open URL&#39;, title=&#39;URL Notification&#39;, open=&#39;https:\/\/www.example.com&#39;)<\/p>\n<h2><strong>\u81ea\u5b9a\u4e49\u56fe\u6807<\/strong><\/h2>\n<p>Notifier.notify(&#39;Notification with icon&#39;, title=&#39;Icon Notification&#39;, appIcon=&#39;path\/to\/icon.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528APPLE SCRIPT<\/p>\n<\/p>\n<p><p>\u5982\u679c\u4e0d\u60f3\u4f9d\u8d56\u5916\u90e8\u5e93\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528AppleScript\u901a\u8fc7Python\u53d1\u9001\u901a\u77e5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528Python\u8c03\u7528AppleScript<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528Python\u7684subprocess\u6a21\u5757\u6765\u6267\u884cAppleScript\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>def send_notification(title, message):<\/p>\n<p>    script = f&#39;display notification &quot;{message}&quot; with title &quot;{title}&quot;&#39;<\/p>\n<p>    subprocess.run([&#39;osascript&#39;, &#39;-e&#39;, script])<\/p>\n<p>send_notification(&quot;Notification Title&quot;, &quot;Hello, this is a notification!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>display notification<\/code>\u662fAppleScript\u4e2d\u7684\u547d\u4ee4\uff0c\u7528\u4e8e\u663e\u793a\u901a\u77e5\u3002<code>subprocess.run<\/code>\u7528\u4e8e\u6267\u884cAppleScript\u547d\u4ee4\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u201cTERMINAL-NOTIFIER\u201d\u5de5\u5177<\/p>\n<\/p>\n<p><p>\u201cterminal-notifier\u201d\u662f\u4e00\u4e2a\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u53d1\u9001Mac\u901a\u77e5\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5terminal-notifier<\/strong><\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5terminal-notifier\u5de5\u5177\u3002\u53ef\u4ee5\u901a\u8fc7Homebrew\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">brew install terminal-notifier<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528Python\u8c03\u7528terminal-notifier<\/strong><\/li>\n<\/ol>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7subprocess\u6a21\u5757\u5728Python\u811a\u672c\u4e2d\u8c03\u7528terminal-notifier\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>def send_notification(title, message):<\/p>\n<p>    subprocess.run([&#39;terminal-notifier&#39;, &#39;-title&#39;, title, &#39;-message&#39;, message])<\/p>\n<p>send_notification(&quot;Notification Title&quot;, &quot;Hello, this is a notification!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>terminal-notifier<\/code>\u7684<code>-title<\/code>\u548c<code>-message<\/code>\u9009\u9879\u7528\u4e8e\u6307\u5b9a\u901a\u77e5\u7684\u6807\u9898\u548c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ecb\u7ecd\u4e86\u4e09\u79cd\u5728Mac\u4e0a\u53d1\u9001\u901a\u77e5\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528pync\u5e93\u3001AppleScript\u548cterminal-notifier\u5de5\u5177\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u60a8\u7684\u9700\u6c42\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u7b80\u5355\u6613\u7528<\/strong>\uff1a\u5982\u679c\u60a8\u5e0c\u671b\u5feb\u901f\u5b9e\u73b0\u901a\u77e5\u529f\u80fd\uff0c\u4e14\u4e0d\u4ecb\u610f\u5b89\u88c5\u7b2c\u4e09\u65b9\u5e93\uff0c\u5efa\u8bae\u4f7f\u7528pync\u5e93\u3002<\/li>\n<li><strong>\u65e0\u4f9d\u8d56\u6027<\/strong>\uff1a\u5982\u679c\u60a8\u5e0c\u671b\u5c3d\u91cf\u51cf\u5c11\u5916\u90e8\u4f9d\u8d56\uff0c\u53ef\u4ee5\u4f7f\u7528AppleScript\u3002<\/li>\n<li><strong>\u547d\u4ee4\u884c\u5de5\u5177<\/strong>\uff1a\u5982\u679c\u60a8\u4e60\u60ef\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u6216\u8005\u9700\u8981\u5728\u811a\u672c\u4e2d\u9891\u7e41\u53d1\u9001\u901a\u77e5\uff0cterminal-notifier\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002<\/li>\n<\/ul>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Mac\u4e0a\u5b9e\u73b0Python\u901a\u77e5\u529f\u80fd\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u573a\u666f\u548c\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002\u65e0\u8bba\u60a8\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u901a\u77e5\u529f\u80fd\u96c6\u6210\u5230\u60a8\u7684Python\u5e94\u7528\u4e2d\uff0c\u63d0\u9ad8\u5e94\u7528\u7684\u4ea4\u4e92\u6027\u548c\u7528\u6237\u4f53\u9a8c\u3002\u901a\u8fc7\u5408\u7406\u5730\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u548c\u6280\u672f\uff0c\u60a8\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u548c\u5b9a\u5236\u5316\u7684\u901a\u77e5\u529f\u80fd\uff0c\u4f8b\u5982\u6761\u4ef6\u89e6\u53d1\u901a\u77e5\u3001\u6279\u91cf\u53d1\u9001\u901a\u77e5\u7b49\uff0c\u4ece\u800c\u66f4\u597d\u5730\u6ee1\u8db3\u5e94\u7528\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0Mac\u7684\u901a\u77e5\u529f\u80fd\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>osascript<\/code>\u547d\u4ee4\u6216\u8005\u7b2c\u4e09\u65b9\u5e93\u5982<code>Plyer<\/code>\u6765\u5b9e\u73b0Mac\u7684\u901a\u77e5\u529f\u80fd\u3002<code>osascript<\/code>\u5141\u8bb8\u4f60\u76f4\u63a5\u8c03\u7528AppleScript\uff0c\u800c<code>Plyer<\/code>\u63d0\u4f9b\u4e86\u4e00\u4e2a\u8de8\u5e73\u53f0\u7684API\uff0c\u4fbf\u4e8e\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u5b9e\u73b0\u901a\u77e5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528<code>Plyer<\/code>\u7684\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">from plyer import notification\n\nnotification.notify(\n    title=&#39;\u901a\u77e5\u6807\u9898&#39;,\n    message=&#39;\u8fd9\u662f\u901a\u77e5\u7684\u5185\u5bb9&#39;,\n    app_name=&#39;\u4f60\u7684\u5e94\u7528\u540d\u79f0&#39;\n)\n<\/code><\/pre>\n<p><strong>\u4f7f\u7528Python\u53d1\u9001Mac\u901a\u77e5\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u53d1\u9001\u901a\u77e5\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u4f60\u7684Python\u73af\u5883\u5df2\u7ecf\u5b89\u88c5\u4e86\u76f8\u5e94\u7684\u5e93\uff0c\u6bd4\u5982<code>Plyer<\/code>\u3002\u6b64\u5916\uff0c\u7528\u6237\u53ef\u80fd\u9700\u8981\u5728\u7cfb\u7edf\u8bbe\u7f6e\u4e2d\u5141\u8bb8\u4f60\u7684\u5e94\u7528\u53d1\u9001\u901a\u77e5\u3002\u786e\u4fdd\u6d4b\u8bd5\u73af\u5883\u7684\u6743\u9650\u8bbe\u7f6e\u6b63\u786e\uff0c\u8fd9\u6837\u901a\u77e5\u624d\u80fd\u6b63\u5e38\u663e\u793a\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u81ea\u5b9a\u4e49Mac\u901a\u77e5\u7684\u6837\u5f0f\u548c\u884c\u4e3a\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u4f7f\u7528AppleScript\u53ef\u4ee5\u5bf9\u901a\u77e5\u8fdb\u884c\u66f4\u7ec6\u81f4\u7684\u81ea\u5b9a\u4e49\uff0c\u6bd4\u5982\u8bbe\u7f6e\u56fe\u6807\u3001\u58f0\u97f3\u7b49\u3002\u901a\u8fc7<code>osascript<\/code>\u547d\u4ee4\uff0c\u4f60\u53ef\u4ee5\u7f16\u5199AppleScript\u811a\u672c\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u901a\u77e5\u6548\u679c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-bash\">osascript -e &#39;display notification &quot;\u8fd9\u662f\u901a\u77e5\u5185\u5bb9&quot; with title &quot;\u901a\u77e5\u6807\u9898&quot;&#39;\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u5728\u901a\u77e5\u4e2d\u6dfb\u52a0\u66f4\u591a\u4e2a\u6027\u5316\u7684\u5143\u7d20\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u5b9e\u73b0Mac\u901a\u77e5\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u201cpync\u201d\u5e93\u3001AppleScript\u3001\u6216\u201cterminal-no [&hellip;]","protected":false},"author":3,"featured_media":973148,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/973141"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=973141"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/973141\/revisions"}],"predecessor-version":[{"id":973151,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/973141\/revisions\/973151"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/973148"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=973141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=973141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=973141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}