How to automate XCode project alterations? - xcode

I have a tedious task that I need to repeat every time I build my iPhone project for sending to non-programmer colleagues: all libraries and headers that I reference outside the project's folder need to be copied into the project folder or a subfolder thereof, so I can zip up the source and pass it on.
I'm aware that you can automate XCode with AppleScript, but I can't for the life of me find any proper documentation of the commands available. If I google XCode AppleScript automation, I just get a bunch of hits on writing AppleScript apps in Xcode.
Any help will be greatly appreciated!

here's a demo to get you started. since you know how to locate the dictionary, the rest may be easy if you are comfortable using AppleScript:
tell application "Xcode"
-- of course, use your project's name instead of Alias
set projectName to "Alias"
set xcProject to project projectName
repeat with idx from 1 to the count of (get every file reference of xcProject)
set fileRef to (file reference idx) of xcProject
get path of fileRef
get id of fileRef
get name of fileRef
get properties of fileRef
set newPath to path of fileRef
-- add logic and alterations here
set path of fileRef to newPath
-- ...
end repeat
end tell

based on your description, it would be easier to create a custom target in xcode to perform this task. xcode can run all sorts of unix utilities for copying, zipping, etc. soo... shell scripting should be much easier. you can also set this target up as a dependency if you'd like, so it always stays up to date (but a little slow for regular development, tbh.
yeah, you can use applescript with xcode in many cases, but it is going to be much more difficult for the task you've outlined.

Related

How do I reference a folder that I copied with Automator action in a subsequent AppleScript?

I have close to zero AppleScript experience and very little Automator experience too, so please bear this in mind. What I'm asking might be REALLY obvious to you, but I'm somewhat stuck.
I want to create a workflow (saved as an App) where I can drag a folder and it does a few things:
Copies the source folder
Sets some background colors on the copied folders
Deletes any folders that are empty
I found a script that does the deletion, but it was set to reference a specific folder. I want it to reference the folder that I have just copied:
tell application "Finder"
set the_items to every folder of entire contents of folder "$CopiedFolderHere"
...
Image of the Workflow is here
I would really appreciate it if someone can tell me what I need to tweak in the script to allow it to make the deletions on the newly-copied folder.
Thanks :)

Aperture export import script

With macOS Catalina dropping support for Aperture, I need to move my library to a different application. Mylio has a great import tool that carries over almost all of the library structure and metadata but loses some of the adjustments. To retain those, I'm trying to write an Apple script that exports all images (with adjustments applied) in my library and reimports them into the same project before deleting the original.
I think I almost have the first part, exporting all images.
tell application "Aperture"
tell library 1
set projects to (get every project)
repeat with i from 1 to count of projects
tell (item i of projects)
export its every image version naming folders with folder naming policy ¬
"Project Name" using export setting ¬
"JPEG - Original Size" to (choose folder with prompt "Choose an export folder")
end tell
end repeat
end tell
end tell
However, when I try to run this script, I keep getting the error
apertureExportImport.applescript:704:712:
execution error: Aperture got an error: Can’t make {project id "1ixjjya6T+Sb3pFmhRK8Fg"} into type project. (-1700)
I guess the problem is with set projects to (get every project) but I can't find any examples online from which to figure out what else to write here.
I don't use Aperture, so I can only take an educated guess:
From the error message, I can be reasonably sure that project is an AppleScript object, specifically an element that, from your script, I'll assume belongs to a library object. In that case, there will almost certainly be a plural element form named "projects" already defined , which would refer to a collection of (i.e. multiple) elements that are all of type project.
Therefore, the following line:
set projects to (get every project)
is problematic, because you're attempting to redefine a class object that belongs to AppleScript. In fact, projects will be shorthand for every project, so this should already be defined in the way that you want, and simply removing this line completely should be sufficient.
On a separate note, it looks like (from you script) the export command can be passed a collection of objects as its direct parameter, which you’re doing with export its every image.... Assuming this is valid, then it might be able to export all images in all projects at once:
tell application "Aperture" to export every image version in every project of library 1 ¬
naming folders with folder naming policy "Project Name" using export setting ¬
"JPEG - Original Size" to (choose folder with prompt "Choose an export folder")
I got it working in the end (everything except deleting the original images after exporting and reimporting but that I can simply do manually by flagging or color-coding all images the script will handle).
tell application "Aperture"
with timeout of 86400 seconds
activate
set exportSetting to export setting "JPEG - Original Size"
set folderPolicy to folder naming policy "Project Name"
set exportFolder to "/Users/<user>/Desktop"
-- Alternatively, use
-- set exportFolder to (choose folder with prompt "Choose an export folder")
tell library 1
repeat with proj in projects
set imageSel to every image version in proj where flagged is true
export imageSel naming folders with folderPolicy using exportSetting to exportFolder
set importFolder to exportFolder & "/" & name of proj
import importFolder by referencing into proj
end repeat
end tell
end timeout
end tell
One important lesson that was hard to learn is that exportSetting and folderPolicy need to be set in the context of the entire library (i.e. at the very start of the script) rather than on a per-project level to avoid the error
execution error: Aperture got an error: Can’t get folder naming policy "Project Name". (-1728)

How to remove mapping/binding of application in AppleScript

I have some AppleScript which I run via osascript on the command line.
The script itself looks like:
on run argv
set appname to item 1 of argv
set tmp to item 2 of argv
set jsfl_path to POSIX file tmp
if application appname is running then
tell application appname
with timeout of 600 seconds
open jsfl_path
end timeout
end tell
end if
end run
I updated Adobe CC Animate to the latest version. I was previously running the 2018 version. It turns out they renamed the file pattern. It used to be Adobe Animate CC 2018. Now it is Adobe Animate 2019.
Here is where the problem starts. In my script I was sloppy and change the name to Adobe Animate CC 2019.
When I ran the AppleScript, it produced a dialog to Choose Application. In my haste, I accidentally mapped it to the wrong program.
I'd like to remove this mapping. What I cannot find is where this mapping is stored. Does anyone know where this type of mapping gets saved?
It isn’t really stored anywhere, the Script Editor uses your declarations to determine where to load the scripting terminology from. Recompiling the script after making an edit should reset things, otherwise you can restart the Script Editor. Note that you need to use a specific bundle identifier or application name - using a variable to specify the application can be problematic, as the terminology is loaded at compile time.

xcode 4.6 applescript open text file for reading

I am trying to incorporate my applescript into xcode. the script works normally with applescript but not ran in xcode. I am trying to open the file for reading. here is the code
set Location to "US"
set DriverFile to "/Volumes/MacPrintDrivers/" & Location & "DriverInstall.txt"
set DriverInstallFile to POSIX file DriverFile
open for access DriverInstallFile
i have confirmed the file exists and i can display the contents. i can't seem to read this way in Xcode. the error i get is
«script» doesn’t understand the «event rdwropen» message. (error -1708)
ASOC (AppleScript Objective-C) has some problems with scripting addition commands (such as open for access). Sometimes you can work around this by saying tell current application to, e.g. tell current application to open for access. But for further details I suggest you get Shane Stanley's book: http://www.macosxautomation.com/applescript/apps/ He has explored this in great depth.

Create AppleScript for a program that isn't installed on the current computer

I'm trying to make two copies of an AppleScript, one that works for Entourage and one for out Outlook. I only have Entourage installed on the current computer.
According to the info on Microsoft's site, both applications have the same library of AppleScript commands, and I should be able to simply change the application name referenced within the script.
Changing:
Tell application "Microsoft Entourage"
to
Tell application "Microsoft Outlook"
Prevents me from saving the script because I don't have outlook installed on this computer. Is there any way around this? Do I need to use a text editor to edit the actual script file and change it there?
Thanks!
The following work-around may do the trick. On the computer where Entourage is installed, a using terms directive will let you compile the script, even if Outlook is not installed:
set theApp to a reference to application "Microsoft Outlook"
using terms from application "Microsoft Entourage"
tell theApp
get version
...
end tell
end using terms from
Upon compiling and saving the script the AppleScript Editor will bug you about the missing Outlook application, but it will nevertheless produce a compiled AppleScript file (.scpt).
Applescript is a pre-complied file format, meaning that every time you click "Save" it runs through a series of steps to ensure the script will work, but just short of actually running through the script's logic. Part of those steps is to look for the application to see if it exists on the Mac.
In short, if you want to save the script as an Applescript, you need the target application installed, otherwise you can save the script as a text file and move the file over to the target Mac to save as an Applescript over there.
It should be possible to make one script that works with both Entourage and Outlook, without bugging you if one isn't found either when you compile or when you run. I don't have either Entourage or Outlook but it should work like this:
using terms from application "Microsoft Entourage"
script theScript
tell application "Finder" to try
set theApp to application file id "Entourage's Bundle ID" as text
on error
set theApp to application file id "Outlook's Bundle ID" as text
end try
tell application theApp
-- do stuff
end tell
end script
end using terms from
store script theScript in "MyScript.scpt"
"using terms from" is only relevant when compiling the script - it isn't needed when running, though for some reason you'll still get bugged if that app isn't found. So by wrapping it around a script object and then writing out that script to file, the resultant script will still run but won't contain "using terms from" and so won't bug the user.
For getting a reference to the right app, Finder can look for it by ID and simply error if it isn't found rather than bugging the user. You'll need to insert the proper ID's there, I don't know what they are.

Resources