Skip to content

Commit

Permalink
Update safari extension (#16196)
Browse files Browse the repository at this point in the history
- Add new command to copy tab as link
- Initial commit
  • Loading branch information
thomaspaulmann authored Jan 6, 2025
1 parent c6275ce commit ad15ed4
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 14 deletions.
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`);
}

0 comments on commit ad15ed4

Please sign in to comment.