Xcode provides method folding option for the current file. But is there any way to apply this to all the .m files in the project ?
p.s: I tried Xcode run scripts & Xcode plugin development, but failed to comeup with a proper solution
Here's an AppleScript for the purpose. Caveat:
System Events is required.
The script probably needs changes if you have multiple workspaces open or your workspace contains multiple projects.
You may need to have something else than an .m file selected in the project navigator.
-- Collect all Objective-C file references from the given group.
on handleGroup(theGroup, codeRefs)
tell application "Xcode"
set fileRefs to theGroup's file references
repeat with x in fileRefs
if x's file kind is equal to "sourcecode.c.objc" then
copy x to end of codeRefs
end if
end repeat
set subgroups to theGroup's groups
repeat with x in subgroups
my handleGroup(x, codeRefs)
end repeat
end tell
end handleGroup
on moveFocusToProjectNavigator()
tell application "System Events"
tell process "Xcode"
set frontmost to true
tell menu bar item "Navigate" of menu bar 1
tell menu 1
click menu item "Reveal in Project navigator"
end tell
end tell
end tell
end tell
end moveFocusToProjectNavigator
on moveFocusToNextArea()
tell application "System Events"
tell process "Xcode"
set frontmost to true
tell menu bar item "Navigate" of menu bar 1
tell menu 1
click menu item "Move Focus to Next Area"
end tell
end tell
end tell
end tell
end moveFocusToNextArea
-- Simulate pressing the up arrow.
on selectNextItem()
tell application "System Events"
tell process "Xcode"
key code 126
end tell
end tell
end selectNextItem
on fold(shouldFold)
set theCommand to "Fold Methods & Functions"
if shouldFold is equal to 0 then
set theCommand to "Unfold All"
end if
-- Fold the code by using the menu item.
tell application "System Events"
tell process "Xcode"
set frontmost to true
tell menu bar item "Editor" of menu bar 1
tell menu 1
tell menu item "Code Folding"
tell menu 1
click menu item theCommand
end tell
end tell
end tell
end tell
end tell
end tell
end fold
on run
-- Set to one to fold, zero to unfold.
set shouldFold to 1
tell application "Xcode"
set ws to every workspace document
set theWorkspace to item 1 of ws
tell theWorkspace
set pr to item 1 of projects
set codeRefs to {}
my handleGroup(pr, codeRefs)
-- Open each document and fold the code.
set thePaths to {}
repeat with x in codeRefs
copy x's full path to the end of thePaths
end repeat
end tell
-- If we only have one path, a new workspace won't be opened.
if 1 is equal to (count of thePaths) then
open item 1 of thePaths
my moveFocusToNextArea()
my fold(shouldFold)
else
open thePaths
set ws to every workspace document
set theWorkspace to item 1 of ws
my fold(shouldFold)
repeat (count of theWorkspace's file references) - 1 times
my moveFocusToProjectNavigator()
my selectNextItem()
my moveFocusToNextArea()
my fold(shouldFold)
end repeat
-- If we don't wait before closing the window, the last document
-- won't be folded.
tell application "System Events" to delay 3
tell theWorkspace to close
end if
end tell
end run
For the sake of completeness, here's an earlier attempt. It is quite slow since it needs to open one document by URL at a time.
-- Collect all Objective-C file references from the given group.
on handleGroup(theGroup, codeRefs)
tell application "Xcode"
set fileRefs to theGroup's file references
repeat with x in fileRefs
if x's file kind is equal to "sourcecode.c.objc" then
copy x to end of codeRefs
end if
end repeat
set subgroups to theGroup's groups
repeat with x in subgroups
my handleGroup(x, codeRefs)
end repeat
end tell
end handleGroup
on run
tell application "Xcode"
set ws to every workspace document
tell item 1 of ws
set pr to item 1 of projects
set codeRefs to {}
my handleGroup(pr, codeRefs)
-- Open each document and fold the code.
repeat with x in codeRefs
set thePath to x's full path
set doc to open thePath
tell doc
activate
end tell
-- Fold the code by using the menu item.
tell application "System Events"
tell process "Xcode"
set frontmost to true
tell menu bar item "Editor" of menu bar 1
tell menu 1
tell menu item "Code Folding"
tell menu 1
click menu item "Fold Methods & Functions"
end tell
end tell
end tell
end tell
end tell
end tell
end repeat
end tell
end tell
end run
Maybe much easier and better solution will be use hotkey:
When you open some .m file, just press
Cmd + Alt + Shirt + <- to fold
or
Cmd + Alt + Shirt + -> to fold
Done and dusted.
I wrote a xCode plugin which easily does the work.
plugin: https://github.com/ThilinaHewagama/HCTAutoFolding
Related
Hi(first I'm not good at English.. sorry)
my device is 2019 16-inch MacBook Pro,
my os is Ventura 13.1
and my code is running on automator
my code is
set brightness to 0.7
tell application "System Settings"
reveal anchor "displaysDisplayTab" of pane "com.apple.preference.displays"
end tell
tell application "System Events" to tell process "System Preferences" to tell window "내장 Retina 디스플레이"
set tryUntil to (current date) + 2 -- 2 seconds
repeat
try
tell group 1 to tell tab group 1
set value of value indicator 1 of slider 1 to brightness
end tell
exit repeat
on error errorMessage
try
tell tab group 1
set value of value indicator 1 of slider 1 to brightness
end tell
exit repeat
end try
if ((current date) > tryUntil) then
error errorMessage
end if
end try
end repeat
end tell
this code from this link, very bottom
https://apple.stackexchange.com/questions/272531/dim-screen-brightness-of-mbp-using-applescript-and-while-using-a-secondary-mon
but when I click, this error image came out.. how can I fix script to change monitor brightness? Thank you.
error image when above applescript try run
perfectly running AppleScript on my computer
I've been trying to follow this youtube tutorial about a Sliding Door.
https://www.youtube.com/watch?v=8ZpD8t8kQC8&ab_channel=Terra
I followed all of the tutorial but the Door still doesn't open.
Here is the script:
animator = require(script.Parent.MainDoor.Animator)
script.Parent.Button.ClickDetector.MouseClick:Connect(function()
script.Parent.Button.CLickDetector.MaxActivationDistance = 0
wait(1)
script.Parent.MainDoor.Open:Play()
wait(0.1)
animator.DoorOpen:Play()
end)
Can someone please help me. Thanks.
Are there any errors in the output? Go to View > Output, to enable the window
I believe there might be a spelling error on line 4 'CLickDetector'. The l was not capital on line 3 so that might be throwing you an error since names are case-sensitive.
script.Parent.Button.CLickDetector.MaxActivationDistance = 0
Hope it helped!
In response to:
main door is not a valid member of model workspace.sliding door
Make sure that the main door is underneath the SlidingDoor model, and not outside it.
Paste this in the command bar(View > Command Bar), to find out what the parent is:
for _,v in ipairs(workspace:GetDescendants()) do
if string.lower(v.Name) == "maindoor" then
print("Found main door, parent is: " .. tostring(v.Parent))
end
end
The result will be shown in the output.
I am running automated UI tests that depend on a clean build for every run.
I'd like to add a pre-action run script that clears out anything in the Application Support directory (e.g. /Users/username/Library/Application Support/iPhone Simulator/7.1/Applications/58A5DCF7-689B-4D13-B178-A88CDE33512/Library/Application Support)
Is there an Xcode variable that makes it easy to get to that directory? Anything like $(BUILD_DIR)?
Here is what works for me:
#!/bin/bash
# `menu_click`, by Jacob Rus, September 2006
#
# Accepts a list of form: `{"Finder", "View", "Arrange By", "Date"}`
# Execute the specified menu item. In this case, assuming the Finder
# is the active application, arranging the frontmost folder by date.
osascript <<SCRIPT
on menu_click(mList)
local appName, topMenu, r
-- Validate our input
if mList's length < 3 then error "Menu list is not long enough"
-- Set these variables for clarity and brevity later on
set {appName, topMenu} to (items 1 through 2 of mList)
set r to (items 3 through (mList's length) of mList)
-- This overly-long line calls the menu_recurse function with
-- two arguments: r, and a reference to the top-level menu
tell application "System Events" to my menu_click_recurse(r, ((process appName)'s ¬
(menu bar 1)'s (menu bar item topMenu)'s (menu topMenu)))
end menu_click
on menu_click_recurse(mList, parentObject)
local f, r
-- `f` = first item, `r` = rest of items
set f to item 1 of mList
if mList's length > 1 then set r to (items 2 through (mList's length) of mList)
-- either actually click the menu item, or recurse again
tell application "System Events"
if mList's length is 1 then
click parentObject's menu item f
else
my menu_click_recurse(r, (parentObject's (menu item f)'s (menu f)))
end if
end tell
end menu_click_recurse
application "iPhone Simulator" activate
menu_click({"iPhone Simulator", "iOS Simulator", "Reset Content and Settings…"})
tell application "System Events"
tell process "iPhone Simulator"
tell window 1
click button "Reset"
end tell
end tell
end tell
SCRIPT
Taken from: https://stackoverflow.com/a/14811280/1041311
Also note that it wonk work for the first time, since you need to allow Xcode to modify folders. At first run there will be dialog which will take you to System preferences where you need to give Xcode that permissions.
A total noob in Applescript, please bear with me…
We have a process where we use System Events to control the Print dialog in Acrobat X. This works fine; we can "click" the Print button.
Now, we have to wait until the document is printed. While the document prints, a dialog opens, with title "Print", a progress bar and a Cancel button. We can only continue when this window closes.
So far, I have not been successful with that wait; the Applescript continues and that messes up the process.
What I have currently is (note this is part of a bigger script, and variables are defined and appear to be valid.
We have Acrobat active, and the Print dialog is open:
tell application "System Events"
tell process "Acrobat"
-- now we set all the options in the Print dialog,
-- which is in the window Print
click button "OK" of window "Print
end tell
end tell
delay 5
-- this gives Acrobat time to get printing and to open that print dialog window
repeat while exists window "Print"
delay 1
end repeat
close active doc saving no
I also tried to put that code in a Timeout, but no chance.
Now, I am stuck, but I am sure it is a stupid beginner's error.
Another note: I was not able to get the name of this "Print" window using UIElementInspector.
Thanks a lot in advance for any advice.
Is your code enclosed in some tell application block that you haven't reported here?
It should work if you move the repeat loop into the tell process block:
tell application "System Events"
tell process "Acrobat"
-- now we set all the options in the Print dialog,
-- which is in the window Print
click button "OK" of window "Print"
delay 5
-- this gives Acrobat time to get printing and to open that print dialog window
repeat while exists window "Print"
delay 1
end repeat
end tell
end tell
close active doc saving no
I have an Applescript that is working on my computer, but not on my colleague's. I get two errors when manipulating paths: -10004 and -10000. I have an idea on how to solve this, but first I'd like to understand those error codes.
Here is the script (I removed useless part, the full version is on github):
-- export all layers to image files
-- Settings
property exportFileExtension : "png"
property ADD_CANVAS_NUMBER : true
-- End of Settings
on file_exists(FileOrFolderToCheckString)
try
alias FileOrFolderToCheckString
return true
on error
return false
end try
end file_exists
tell application "OmniGraffle Professional 5"
set theWindow to front window
set theDocument to document of theWindow
set theFilename to name of theDocument
-- remove .graffle
-- FIRST ERROR IS HERE -10004
set theFilename to text 1 thru ((offset of "." in theFilename) - 1) of theFilename
set export_folder to (choose folder with prompt "Pick the destination folder") as string
set export_folder to export_folder & theFilename & ":"
-- create folder
if file_exists(export_folder) of me then
try
display alert "The file already exists. Do you want to replace it?" buttons {"Cancel", "Erase"} cancel button 1
on error errText number errNum
if (errNum is equal to -128) then
return
end if
end try
-- deletes the folder (necessary because some layers may have been renamed
do shell script "rm -rf " & quoted form of POSIX path of export_folder
else
-- creates the folder
do shell script "mkdir -p " & quoted form of POSIX path of export_folder
end if
set canvasCount to count of canvases of theDocument
set i to 0
repeat with canvasNumber from 1 to canvasCount
set theCanvas to canvas canvasNumber of theDocument
set canvas_name to name of theCanvas
set canvas of theWindow to theCanvas
set layerCount to count of layers of theCanvas
-- ...
set area type of current export settings to current canvas
set draws background of current export settings to false
set include border of current export settings to false
set canvas_filename to ""
-- ...
set canvas_filename to canvas_filename & canvas_name
repeat with layerNumber from 1 to layerCount
set theLayer to layer layerNumber of theCanvas
if (theLayer is prints) and (class of theLayer is not shared layer) then
set layer_name to name of theLayer as string
set filename to canvas_filename & " - " & layer_name & "." & exportFileExtension
set export_filename to export_folder & filename
-- show the layer, export, then hide the layer
if character 1 of layer_name is not "*" then
set visible of theLayer to true
-- SECOND ERROR IS HERE -1000
save theDocument in export_filename
set visible of theLayer to false
end if
end if
end repeat
end repeat
end tell
Here is the log:
tell application "OmniGraffle Professional 5"
get window 1
--> window id 5032
get document of window id 5032
--> document "MSD.graffle"
get name of document "MSD.graffle"
--> "MSD.graffle"
offset of "." in "MSD.graffle"
--> error number -10004
end tell
tell current application
offset of "." in "MSD.graffle"
--> 4
end tell
tell application "OmniGraffle Professional 5"
choose folder with prompt "Pick the destination folder"
--> alias "Macintosh HD:Users:Romain:Desktop:Temp:"
display alert "The file already exists. Do you want to replace it?" buttons {"Cancel", "Erase"} cancel button 1
--> {button returned:"Erase"}
do shell script "rm -rf '/Users/Romain/Desktop/Temp/MSD/'"
--> error number -10004
end tell
tell current application
do shell script "rm -rf '/Users/Romain/Desktop/Temp/MSD/'"
--> ""
end tell
tell application "OmniGraffle Professional 5"
...
...
save document "MSD.graffle" in "Macintosh HD:Users:Romain:Desktop:Temp:MSD:1- Navigation - 1Layout.png"
--> error number -10000
Result:
error "OmniGraffle Professional 5 got an error: AppleEvent handler failed." number -10000
Thanks!
I updated the script but I still get error -10000. Here are the modified lines:
save theDocument in file exportFilename
and
-- Create folder if does not exist, remove it otherwise
-- Shell script should not be executed inside tell application block
if file_exists(export_folder) of me then
try
display alert "The file already exists. Do you want to replace it?" buttons {"Cancel", "Erase"} cancel button 1
on error errText number errNum
if (errNum is equal to -128) then
return
end if
end try
tell me
-- Delete the folder
do shell script "rm -rf " & quoted form of POSIX path of export_folder
end tell
else
tell me
-- Create the folder
do shell script "mkdir -p " & quoted form of POSIX path of export_folder
end tell
end if
Errors -10000 - -10015 are event registry errors.
Error -10000 is not a target error per se, because it will throw an -1708 in those cases mostly. most of the time it is not a target error but an incomplete command or wrong usage of brackets. What if you use:
save theDocument in file export_filename
Error -10004 is a privilege violation error, which mean you're doing something with the file that isn't allowed. Probably you're not allowed to remove the file and do shell script command should always be used outside tell application blocks. The problem is that the target application can run as another user than the script. I'm not saying it is the error but there is a chance that it this is the problem. Otherwise you simply heve not enough privileges and you need to ask the user for administrator privileges.
do shell script "do something" with administrator privileges.
I haven't found where those error codes are documented, but they mainly deal with events that the targeted application isn't able to do. The first two errors -10004 are from using a Standard Additions command inside an application tell statement (offset and do shell script) - the application doesn't know what those commands are, passes the error up the chain to AppleScript, but AppleScript knows what they are and does it.
I don't have OmniGraffle, but the last error is telling you that the save command couldn't be performed, probably due to a problem with the destination not being a file specifier - it is just a text string, so you will probably have to coerce it into something that the command wants.