Obsidian Paste from Chrome

Hi Nick, wondering if you cud help me with this script. This extension installs fine, pops up when text is selected, but a cross appears, and does nothing

  1. Can you plz point me how to debug this
  2. Can youcorrect the script, cos not sure if “popclip.openApp(“Obsidian”)" exists or works

cheers

#popclip
name: Obsidian Markdown Link
identifier: com.CJ.popclip.obsidian.markdownlink
description: Paste a markdown link with @added(date) into Obsidian
icon: iconify:simple-icons:obsidian
after: paste-result
javascript: |
  // Get selected text if any
  let title = popclip.input.text && popclip.input.text.trim();

  // If no selection, fall back to browser tab title
  if (!title) {
    title = context.browserTitle || "Untitled";
  }

  // Get browser URL
  const url = context.browserUrl || "";

  // Format today's date as YYYY-MM-DD
  const today = new Date().toISOString().slice(0, 10);

  // Build markdown link
  const markdownLink = `[${title}](${url}) @added(${today})`;

  // Activate Obsidian
  popclip.openApp("Obsidian");

  // delay to ensure Obsidian is active
  setTimeout(() => {
    popclip.pasteText(markdownLink);
  }, 150);

I wasn’t able to simply open Obsidian and paste, but I did come up with a few approaches in examples #1-5 in the script below. Your original fail was due to missing references to the ‘popclip’ interface. A better person than I might also include error checking, but that’s not the goal here I think. best, Phil.

#popclip
name: Obsidian Markdown Link
identifier: com.CJ.popclip.obsidian.markdownlink
description: Paste a markdown link with @added(date) into Obsidian
icon: iconify:simple-icons:obsidian
# after: "paste-result" // not needed
javascript: |
  // Get selected text if any
  let title = popclip.input.text && popclip.input.text.trim();

  // If no selection, fall back to browser tab title
  if (!title) {
    const title = popclip.context.browserTitle || "Untitled";
  }

  // Get browser URL
  const url = popclip.context.browserUrl || "";

  // Format today's date as YYYY-MM-DD
  const today = new Date().toISOString().slice(0, 10);

  // Build markdown link
  const markdownLink = `[${title}](${url}) @added(${today})`;

  // Activate Obsidian
  // popclip.openApp("Obsidian");   // does not exist

  // no timeout needed even if Obsidian is not open
  // fyi this boilerplate can be replaced with 'await sleep(150)' above the popclip action;
  // delay to ensure Obsidian is active 
  //setTimeout(() => {
  //  popclip.pasteText(markdownLink);
  //}, 150);

  // 0. Vault and note config
  const vaultName = encodeURIComponent("Vault");
  const noteName = encodeURIComponent("Popclip Imported Links");
  const content = encodeURIComponent(markdownLink);
  const appendContent = encodeURIComponent(`\n ${markdownLink}`);

  // 1. NEW Note based on selected text
  popclip.openUrl(`obsidian://new?vault=${vaultName}&name=${title}&content=${content}`);

  // 2. NEW named Note
  popclip.openUrl(`obsidian://new?vault=${vaultName}&name=${noteName}&content=${content}`);
  
  // 3. NEW Daily Note - core plugin Daily Notes should be enabled
  popclip.openUrl(`obsidian://daily?vault=${vaultName}&content=${content}`);

  // 4. APPEND to named existing note
  // prerequisite = Advanced URI community plugin needs to be installed
  popclip.openUrl(`obsidian://advanced-uri?vault=${vaultName}&filepath=${noteName}.md&data=${appendContent}&mode=append`);

  // 5. Open Obsidian as is and paste manually
  popclip.openUrl("obsidian://");
  pasteboard.text = markdownLink;
  // now paste manually

  // 6. :( APPEND to current note - shell maybe?

  // That's a bingo
  popclip.showSuccess();

(edited to fix typo)

Thanks Phil, for looking at this for me. I wud try this evening when at home. I wrote an AppleScript that works for me. I call it from the Keyboard Maestro conflict palette. I thought I cud use popclip, as it wud be more intuitive to use. select text => popclip.

I usually have Obsidian open all the time, and I usually would like to paste where the cursor is, but I don’t think Obsidian has a way of knowing the cursor position.

Is there a way to call system events from popclip & activate Obsidian to paste? [This is what I do in my applescript ]

Cheers

We can call Applescript directly, or using “osascript -e…” via bash/zsh.

I got the following to paste at the cursor using Applescript directly. I’m going to add this to my active tools!

#popclip
name: Copy to Obsidian
identifier: com.CJ.popclip.obsidian.markdownlink
description: Copy formatted text and open Obsidian
icon: iconify:simple-icons:obsidian
actions:
  - title: Markdown Link
    applescript: |
      set selectedText to "{popclip text}"
      set browserUrl to "{popclip browser url}"
      set browserTitle to "{popclip browser title}"
      
      -- Use selected text if available, otherwise use browser title
      if selectedText is not "" then
        set linkTitle to selectedText
      else if browserTitle is not "" then
        set linkTitle to browserTitle
      else
        set linkTitle to "Untitled"
      end if

      set todayDate to do shell script "date '+%Y-%m-%d'"
      
      -- Build markdown link
      set markdownLink to "[" & linkTitle & "](" & browserUrl & ") @added(" & todayDate & ")"
      
      set the clipboard to markdownLink

      tell application "Obsidian" to activate
      -- delay 0.3 -- optional
      
      -- Paste at cursor
      tell application "System Events"
        keystroke return
        keystroke "v" using {command down}
      end tell

Hey Phil, this works perfectly. i have AsObjc script which i use regularly. I did try to call it by this, but it did not work

# popclip
name: Create Script
icon: square filled SE
applescript: |
  set myScriptPath to ((path to home folder as text) & "Dropbox (Personal):Automation Syncs:PopClip:popClip Scripts:ObsMarkDown.scpt")
  set myScript to load script file myScriptPath
  myScript's makeScript("{popclip text}")
  myScript's newDocument("{popclip text}", "{popclip url})
1 Like

Also can suggest an option to show popclip on selecting an email, with keyboard shortcuts ?

Not sure if you noticed you’re missing a closing double-quote on that last line.

I did some messing around and found upgrading from classic AppleScript helped resolve errors.

Instead of:

set myScriptPath to path to home folder

I used:

set homeFolder to (POSIX path of (path to home folder))

and then I debugged the value out to a dialog:

# popclip
name: Create Script
icon: square filled SE
applescript: |
  --set myScriptPath to path to home folder
  set homeFolder to (POSIX path of (path to home folder))
  display dialog "Debug: " & homeFolder

Continuing on the path of switching from classic, I ended up with the following: It needs to be debugged/tested on your end.

# popclip
name: Create Script
icon: square filled SE
applescript: |
  set homeFolder to (POSIX path of (path to home folder))
  set myScriptPath to homeFolder & "Dropbox (Personal)/Automation Syncs/PopClip/popClip Scripts/ObsMarkDown.scpt"
  set myScript to load script POSIX file myScriptPath
  tell myScript
    makeScript("{popclip text}")
    newDocument("{popclip text}", "{popclip url}")
  end tell

I hope this helps.

fyi, there a few ways to display debug info in PopClip that I can think of:

display dialog "Debug: " & homeFolder
display notification "Debug value: " & homeFolder with title "Debug"
set logFile to (POSIX path of (path to desktop)) & “debug.log”
  -- debug into Obsidian
  set the clipboard to homeFolder
  tell application "Obsidian" to activate
  tell application "System Events"
    keystroke return
    keystroke "v" using {command down}
  end tell