Skip to content

Instantly share code, notes, and snippets.

@scottjacksonx
Last active December 15, 2015 16:49
Show Gist options
  • Save scottjacksonx/5291653 to your computer and use it in GitHub Desktop.
Save scottjacksonx/5291653 to your computer and use it in GitHub Desktop.
# This is the guts of a Service that lets you select a method in Xcode (either a definition or a call),
# click "Copy Selector", and copy the selector for that method to the pasteboard. For example:
#
# “- (void)bar;” → “bar” on the pasteboard
#
# “- (void)doThing:(type *)thing withOptions:(type*)options” → “doThing:withOptions:” on the pasteboard
#
# “[foo barWithOptions:options]” → “barWithOptions:” on the pasteboard
#
# “[[foo barWithBaz:[baz barWithProperty:thing.backgroundColor]] doThing:thing withOptions:options];” →
# “doThing:withOptions:” on the pasteboard
ARGF.each do |f|
selector = ""
f.gsub!(";", "") # remove semicolon if it's there
if f[0,1] == "["
# method call
nesting_level = f.count("]")
if nesting_level > 1
# get rid of everything before and including the second-last "]"
[0..nesting_level-1].each do |i|
next_closing_bracket_index = f.index("]")
f = f[next_closing_bracket_index..-1]
end
puts f
else
# get rid of the object the method is being called on
f = f.split()[1..-1].join(" ")
end
#sanitise input
f.gsub!("[", "")
f.gsub!("]", "")
f.split.each do |component|
if component.split(":").length > 1
selector += component.split(":")[0] + ":"
else
selector += component.split(":")[0]
end
end
else
# method definition
# sanitise input
f.gsub!(/\s+\*/, "*")
f.gsub!(/\(\w+\)/, "")
f.gsub!("{", "")
f.gsub!("-", "")
f.gsub!("+", "")
f.split.each do |component|
if component.split(":").length > 1
selector += component.split(":")[0] + ":"
else
selector += component.split(":")[0]
end
end
end
#puts selector
IO.popen("pbcopy", "r+").puts selector
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment