How to modify AppleScript to replace path with clipboard content

Hello,
The apple script below works perfectly.
I would like to make 2 variants:
1- replace the path /Users/Ron/Downloads with the clipboard contents (which will be a path)
2- script Nb 2 does 2 things. First is copy selected text (ie simply copy) which will be a path, followed by the script above (reveal path)
thanks in advance for your time and help

tell application "Path Finder"
    activate
    reveal (POSIX file "/Users/Ron/Downloads")
end tell

Try this for pulling in the clipboard when the clipboard is set as a path:

tell application "Path Finder"
	activate
	reveal (POSIX file (the clipboard as Unicode text) as alias)
end tell

For triggering a basic CMD+C copy in an application you could use this, but you will need accessibility enabled for it.

tell application "System Events"
    keystroke "c" using {command down}
end tell

For completeness you could try this more sophisticated version which can support a few more apps in practice - but is probably overkill for the majority of use cases. It all depends on what your source app supports - there are a few out there where CMD+C isn’t supported, and if apps do support direct selection access, that can be fractionally quicker - but usually only noticeable at scale on a modern Mac.

on copySelection()
	-- Ensure System Events has permissions
	tell application "System Events"
		set frontmostApp to name of first process whose frontmost is true
	end tell
	
	-- Direct Selection Access -> Edit Menu Item -> Keyboard Shortcut
	try
		-- Get selection directly - available in many of the mainstream apps
		tell application frontmostApp
			set set the clipboard to selection
		end tell
		log "βœ… Successfully copied via selection in " & frontmostApp
	on error errMsg1
		log "⚠️ Selection method failed: " & errMsg1
		try
			-- Click the "Copy" menu item if it exists
			tell application "System Events"
				tell process frontmostApp
					if exists menu item "Copy" of menu "Edit" of menu bar 1 then
						click menu item "Copy" of menu "Edit" of menu bar 1
						log "βœ… Successfully copied via Edit menu in " & frontmostApp
					else
						error "❌ No 'Copy' menu item found in Edit menu."
					end if
				end tell
			end tell
		on error errMsg2
			log "⚠️ Menu click method failed: " & errMsg2
			try
				-- CMD+C keystroke
				tell application frontmostApp to activate
				delay 0.2 -- Ensure the app is active
				tell application "System Events"
					keystroke "c" using {command down}
				end tell
				log "βœ… Successfully copied via keystroke in " & frontmostApp
			on error errMsg3
				log "❌ All methods failed. Error: " & errMsg3
				display alert "Copy failed in " & frontmostApp message errMsg3
			end try
		end try
	end try
end copySelection

-- Copy selection to the clipboard
copySelection()

Hope that helps.

1 Like

Fantastic !! extremely kind of you !