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

Update safari extension #16196

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions extensions/safari/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Update] - {PR_MERGE_DATE}

- Adds a "Copy Title as Link to Clipboard" command to copy the current Safari tab in Markdown format.

## [Update] - {PR_MERGE_DATE}

- Adds a "Copy to Clipboard" command to copy the current Safari tab url.

## [Chore: Renamed title in Dropdown] - 2024-12-20
Expand Down
11 changes: 9 additions & 2 deletions extensions/safari/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,16 @@
},
{
"name": "copy-to-clipboard",
"title": "Copy to Clipboard",
"title": "Copy URL to Clipboard",
"subtitle": "Safari",
"description": "Copy the current Safari tab url to your clipboard",
"description": "Copy the current Safari tab URL to your clipboard",
"mode": "no-view"
},
{
"name": "copy-title-as-link-to-clipboard",
"title": "Copy Title as Link to Clipboard",
"subtitle": "Safari",
"description": "Copy the current Safari tab title as Markdown link ([title](url)) to your clipboard",
"mode": "no-view"
}
],
Expand Down
24 changes: 24 additions & 0 deletions extensions/safari/src/copy-title-as-link-to-clipboard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Clipboard, closeMainWindow, showToast, Toast } from "@raycast/api";
import { getCurrentTabName, getCurrentTabURL } from "./utils";

export default async function Command() {
try {
await closeMainWindow();

const [title, url] = await Promise.all([getCurrentTabName(), getCurrentTabURL()]);
await Clipboard.copy({ text: `[${title}](${url})`, html: `<a href="${url}">${title}</a>` });

await showToast({
style: Toast.Style.Success,
title: "Copied title as link to clipboard",
});
} catch (error) {
console.error(error);

await showToast({
style: Toast.Style.Failure,
title: "Failed copying title as link to clipboard",
message: error instanceof Error ? error.message : undefined,
});
}
}
22 changes: 10 additions & 12 deletions extensions/safari/src/copy-to-clipboard.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
import { Clipboard, closeMainWindow, showToast, Toast } from "@raycast/api";
import { runAppleScript } from "@raycast/utils";
import { safariAppIdentifier } from "./utils";
import { getCurrentTabURL } from "./utils";

export default async function Command() {
try {
const currentURL = await runAppleScript(`
tell application "${safariAppIdentifier}"
set theURL to URL of current tab of window 1
return theURL
end tell
`);
await Clipboard.copy(currentURL);
await closeMainWindow();

const currentURL = await getCurrentTabURL();
await Clipboard.copy(currentURL);

await showToast({
style: Toast.Style.Success,
title: "Copied URL to Clipboard",
title: "Copied URL to clipboard",
});
} catch (error) {
showToast({
console.error(error);

await showToast({
style: Toast.Style.Failure,
title: "Failed to copy URL",
title: "Failed copying URL to clipboard",
message: error instanceof Error ? error.message : undefined,
});
}
Expand Down
9 changes: 9 additions & 0 deletions extensions/safari/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import osascript from "osascript-tag";
import { URL } from "url";
import { langAdaptor, PinyinHandler } from "./lang-adaptor";
import { HistoryItem, LooseTab } from "./types";
import { runAppleScript } from "@raycast/utils";

type Preferences = {
safariAppIdentifier: string;
Expand Down Expand Up @@ -124,3 +125,11 @@ export const groupHistoryByDay = (groups: Map<string, HistoryItem[]>, entry: His
groups.set(date, group);
return groups;
};

export async function getCurrentTabName() {
return await runAppleScript(`tell application "${safariAppIdentifier}" to return name of front document`);
}

export async function getCurrentTabURL() {
return await runAppleScript(`tell application "${safariAppIdentifier}" to return URL of front document`);
}
Loading