Last active
March 26, 2025 12:07
-
-
Save ralfebert/188a39793a6c45a1af9c23aad752862e to your computer and use it in GitHub Desktop.
Open the file currently open in Xcode at the current editor location in Zed via Hammerspoon (~/.hammerspoon/init.lua)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function dirname(path) | |
return path:match("(.+)/[^/]*$") | |
end | |
hs.hotkey.bind({ "cmd", "alt", "ctrl" }, "Z", function() | |
local xcode = hs.application.find("Xcode") | |
if xcode and xcode:isFrontmost() then | |
local script = [[ | |
tell application "Xcode" | |
set docPath to path of document 1 whose name ends with (word -1 of (get name of window 1)) | |
set currentLines to selected paragraph range of document 1 whose name ends with (word -1 of (get name of window 1)) | |
set L1 to get item 1 of currentLines | |
set L2 to get item 2 of currentLines | |
set projPath to path of active workspace document | |
return projPath & "," & docPath & ":" & L1 & ":" & L2 | |
end tell | |
]] | |
local ok, result = hs.osascript.applescript(script) | |
if ok then | |
local parts = hs.fnutils.split(result, ",") | |
local projPath = dirname(parts[1]) | |
local filePath = parts[2] | |
hs.task.new("/opt/homebrew/bin/zed", nil, { projPath, filePath }):start() | |
else | |
hs.alert.show("Error retrieving paths: " .. (result or "")) | |
end | |
else | |
hs.alert.show("Xcode not active") | |
end | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment