2

我希望从我的 mac 自动发送通知中心消息。有没有办法使用 Growl 或 Applescript 来实现这一点?谢谢!

4

1 回答 1

2

我已经能够使用以下方法编写推文脚本。

此 Applescript 代码用于通过输入文本对话框手动撰写推文,并运行检查和发送推文的 shell 脚本。它不使用来自 Growl 或通知中心的文本。

苹果脚本代码:

set q to display dialog "Tweet:" default answer ""

    set myTweet to text returned of q

    if (length of myTweet) > 140 then
        display dialog "I'm pretty sure that tweet is too long (" & (length of t as text) & " chars)" buttons {"oh"} default button 1
    else
        set e to do shell script "cd /pathTo/folderContaining/;./tweet.sh <yourtwitterhandle> <yourtwitterpassword> \"" & myTweet & "\""
        if e ≠ "" then display dialog e buttons {"OK"} default button 1
    end if

我在字数限制方面遇到了一些问题,所以我通常让我的推文特别短。也许您可以找到一个小于 140 的“长度”限制(请参阅 AS 代码:)if (length of myTweet) > 140 then,它可以始终如一地工作。

这是shell脚本;从http://360percents.com/posts/command-line-twitter-status-update-for-linux-and-mac/修改,

确保将其命名为“ tweet.sh ”并使用 chmod 或 Kilometre.app 使其可执行(请注意,我已注释掉所有报告,因此它完全安静地运行):

#!/bin/bash

#REQUIRED PARAMS (crg - now passed through command line)
username=$1
password=$2
tweet=$3 #must be less than 140 chars

#EXTRA OPTIONS
uagent="Mozilla/5.0" #user agent (fake a browser)
sleeptime=0 #add pause between requests

if [ $(echo "$tweet" | wc -c) -gt 140 ]; then
    echo "[FAIL] Tweet must not be longer than 140 chars!" && exit 1
elif [ "$tweet" == "" ]; then
    echo "[FAIL] Nothing to tweet. Enter your text as argument." && exit 1
fi

touch "cookie.txt" #create a temp. cookie file
#crg - commented out all 'success' echos ... will only return string if error
#GRAB LOGIN TOKENS
#echo "[+] Fetching twitter.com..." && sleep $sleeptime
initpage=$(curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent" "https://mobile.twitter.com/session/new")
token=$(echo "$initpage" | grep "authenticity_token" | sed -e 's/.*value="//' | sed -e 's/" \/>.*//')

#LOGIN
#echo "[+] Submitting the login form..." && sleep $sleeptime
loginpage=$(curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent" -d "authenticity_token=$token&username=$username&password=$password" "https://mobile.twitter.com/session")

#GRAB COMPOSE TWEET TOKENS
#echo "[+] Getting compose tweet page..." && sleep $sleeptime
composepage=$(curl -s -b "cookie.txt" -c "cookie.txt" -L -A "$uagent" "https://mobile.twitter.com/compose/tweet")

#TWEET
#echo "[+] Posting a new tweet: $tweet..." && sleep $sleeptime
tweettoken=$(echo "$composepage" | grep "authenticity_token" | sed -e 's/.*value="//' | sed -e 's/" \/>.*//' | tail -n 1)
update=$(curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent" -d "authenticity_token=$tweettoken&tweet[text]=$tweet&tweet[display_coordinates]=false" "https://mobile.twitter.com/")

#GRAB LOGOUT TOKENS
logoutpage=$(curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent" "https://mobile.twitter.com/account")

#LOGOUT
#echo "[+] Logging out..." && sleep $sleeptime
logouttoken=$(echo "$logoutpage" | grep "authenticity_token" | sed -e 's/.*value="//' | sed -e 's/" \/>.*//' | tail -n 1)
logout=$(curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent" -d "authenticity_token=$logouttoken" "https://mobile.twitter.com/session/destroy")

rm "cookie.txt"
于 2014-01-03T18:58:26.037 回答