使用Applescript自动化Safari:根据URL智能切换到已打开的选项卡

时间:2017-05-27 14:16:31

标签: safari applescript

我尝试使用口述功能使用语音命令自动化我的Mac。我已经得到了以下简单的脚本,即使Safari已关闭,也会打开带有网址的新Safari选项卡:

private class PersonsAdapter extends ArrayAdapter<Person> {

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_entry, null);
            holder = new ViewHolder();
            holder.nameTextView = (TextView) convertView.findViewById(R.id.person_name);
            holder.surnameTextView = (TextView) convertView.findViewById(R.id.person_surname);
            holder.personImageView = (ImageView) convertView.findViewById(R.id.person_image);
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }

        Person person = getItem(position);

        holder.nameTextView.setText(person.getName());
        holder.surnameTextView.setText(person.getSurname());
        //holder.personImageView.setImageBitmap(person.getImage());

        return convertView;
    }
}

效果很好。我可以说&#34; Mac,打开Gmail&#34;它会弹出。我想看看我是否可以改进脚本,并让脚本确定某个站点是否已在另一个选项卡中打开,如果是,则切换到该现有选项卡。有没有办法获取包含我想要的URL的第一个选项卡的编号?

1 个答案:

答案 0 :(得分:2)

甜蜜,感谢https://hea-www.harvard.edu/~fine/OSX/safari-tabs.html的脚本天才我有一个解决方案。我稍微修改了那里的脚本来实现这一点。结果如下:

if application "Safari" is running then
    tell application "Safari"
        activate
    end tell
else
    tell application "Safari"
        activate
        delay 5
    end tell
end if

set searchpat to "mail.google"
set theUrl to "https://mail.google.com/mail/u/0/#inbox"

tell application "Safari"
    set winlist to every window
    set winmatchlist to {}
    set tabmatchlist to {}
    set tabnamematchlist to {}
    repeat with win in winlist
        set ok to true
        try
            set tablist to every tab of win
        on error errmsg
            --display dialog name of win as string
            set ok to false
        end try
        if ok then
            repeat with t in tablist
                if searchpat is in (name of t as string) then
                    set end of winmatchlist to win
                    set end of tabmatchlist to t
                    set end of tabnamematchlist to (id of win as string) & "." & (index of t as string) & ".  " & (name of t as string)
                    --display dialog name of t as string
                else if searchpat is in (URL of t as string) then
                    set end of winmatchlist to win
                    set end of tabmatchlist to t
                    set end of tabnamematchlist to (id of win as string) & "." & (index of t as string) & ".  " & (name of t as string)
                    --display dialog name of t as string
                end if
            end repeat
        end if
    end repeat
    if (count of tabmatchlist) = 1 then
        --display dialog "one!"
        set w to item 1 of winmatchlist
        set t to item 1 of tabmatchlist
        set current tab of w to t
        set index of w to 1
    else if (count of tabmatchlist) = 0 then
        if not (exists current tab of front window) then make new document -- if no window
        tell front window    
                  set current tab to (make new tab at end of tabs with properties {URL:theUrl})
      end tell


    else
        set whichtab to choose from list of tabnamematchlist with prompt "The following tabs match, please select one:"
        set AppleScript's text item delimiters to "."
        if whichtab is not equal to false then
            set tmp to text items of (whichtab as string)
            set w to (item 1 of tmp) as integer
            set t to (item 2 of tmp) as integer
            set current tab of window id w to tab t of window id w
            set index of window id w to 1
        end if
    end if
end tell