copy file into nib Package Contents

I’d like to unlock a compiled nib so it can be edited in Interface Builder. This requires adding 2 dummy files (“classes.nib” & “info.nib”) into the Package Contents of the nib file. This works fine manually…

BUT an applescript to automate the process is failing. How do I tell Finder to duplicate the dummy files into the Package Contents? It’s treating the nib as a file, not a folder???

Getting error: “Error: -1700. Finder got an error: Can’t make alias “*Macintosh:Users:alnoor:Desktop:BasicWindows.nib:” into type folder.”

Basic, I know, but it’s stumping me!

Details below…

The applescript is saved as an app with dummy files copied into Contents/Resources:

--"unlock nib" v1 

--save as app
--requires files "classes.nib" & "info.nib" in Contents/Resources
-----------------------------------------------

set file1 to "classes.nib" as text
set file2 to "info.nib" as text


display dialog "unlock nib?" & return & return & "choose a compiled nib to open with Interface Builder:" buttons {"Cancel", "Choose Nib"} default button 2 with icon 1

tell application "Finder"
	set target_file to (choose file of type {"nib"}) as text
	try
		
		duplicate file (((path to me) as text) & "Contents:Resources:" & file1) to target_file as alias
		
	on error the error_message number the error_number
		display dialog "Error: " & the error_number & ". " & the error_message buttons {"Cancel"} default button 1 with icon 0
	end try
end tell

The dummy file contents are:

classes.nib

'<?xml version="1.0" encoding="UTF-8"?>

'

info.nib

'<?xml version="1.0" encoding="UTF-8"?>

'

I assume that it would not be sufficient to get the wanted behavior but you may look at this edited/commented version.

--"unlock nib" v1 

--save as app
--requires files "classes.nib" & "info.nib" in Contents/Resources
-----------------------------------------------

set file1 to "classes.nib" # no need to coerce as text, it's already text item. # as text
set file2 to "info.nib" # no need to coerce as text, it's already text item.  # as text


display dialog "unlock nib?" & return & return & "choose a compiled nib to open with Interface Builder:" buttons {"Cancel", "Choose Nib"} default button 2 with icon 1

# choose file is part of the OSAX Standard Additions.
# Putting it inside a tell Finder block is odd.
# Choose file returns an alias so it's wasting time to coerce the result into text then coercing it back into alias.
set target_file to (choose file of type {"nib"}) # as text

tell application "Finder"
	
	try
		duplicate file (((path to me) as text) & "Contents:Resources:" & file1) to target_file # See comment above # as alias
		
	on error the error_message number the error_number
		display dialog "Error: " & the error_number & ". " & the error_message buttons {"Cancel"} default button 1 with icon 0
	end try
end tell

Yvan KOENIG (VALLAURIS, France) mardi 9 septembre 2014 15:21:31

Yvan, thanks for your comments. I’ve tidied it up as suggested.

It now works with choose folder, rather than choose file.
(I was thrown because if Interface Builder app is loaded, the nib displays on the desktop with a interface builder nib icon rather than a folder!)

BUT, I’d still like to restrict the choice to a nib, rather than any folder.
I guess I can require the folder name to include “.nib”.

Probably also better to make new files with content of dummy files, rather than copy existing versions from Contents/Resources…

Hello

I didn’t understood that your problem was with choose file.

According to my tests no hope with choose file.
I duplicated a nib file from an application onto the Desktop.
Then, I ran this short script using the official Unique Identifier of such object.

choose file of type {"com.apple.interfacebuilder.document"}

The nib file appeared grayed (disabled) in the dialog.
I checked its structure, it was a flat file.

I repeated the game with a nib whose structure is package and this time I was able to select it.
This one contains a nib file which is a flat file.

So, my understanding is that if you may reach your nib with choose folder you will be able to reach it with the code given above.

Yvan KOENIG (VALLAURIS, France) mardi 9 septembre 2014 16:42:07

Great!

I’d noticed that some nib files do not show Package Contents when control clicked, so they must be flat files.

Yes, I hadn’t realised the problem was with choose file until after my original post.

Thanks for your help.

Still have an issue with choose file vs choose folder:

Does Finder treat a nib as a file or a folder?
It seems to depend on whether Interface Builder.app is present.

I happen to have XCode (including IB) on a removable drive. So if it’s not plugged in, Finder displays a nib as a folder and the script below works (treating it as a folder). BUT if it is plugged in, and IB is running, Finder displays it as an interface builder document and the script below won’t allow selection of the nib (it wants a folder??).

However, if I change the line to:

set target_file to choose file of type {“com.apple.interfacebuilder.document”},

I can select the nib as a file, BUT the script fails on the line:

set theFile to (make file at folder some_location with properties {name:some_name & some_extension}) as text

because it can’t make a file inside another file???

So the script below works, but only if Interface Builder is not present.
A pain, coz it’s to use with IB!

--"unlock nib" v2
-----------------------------------------

set myname1 to "classes"
set myname2 to "info"
set myextension to ".nib"
set mycontent to "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
<dict>
</dict>
</plist>
"

--MAKE FILE
on makefile(some_name, some_extension, some_location, some_content)
	tell application "Finder"
		try
			set theFile to (make file at folder some_location with properties {name:some_name & some_extension}) as text
			set foo to open for access theFile with write permission --write to file
			set extension hidden of file theFile to false --OPTIONALLY HIDE EXTENSION
			(*
		set eof of foo to 0 --ONLY IF OVERWRITING!!
		*)
			write some_content to foo starting at eof --i.e replacing existing text
			close access foo
		on error the error_message number the error_number
			display dialog "Error: " & the error_number & ". " & the error_message buttons {"Cancel"} default button 1 with icon 0
		end try
	end tell
end makefile


--***** SCRIPT *****

--welcome
display dialog "unlock nib?" & return & return & "choose a compiled nib to open with Interface Builder:" buttons {"Cancel", "Choose Nib"} default button 2 with icon 1

set target_file to (choose folder)
my makefile(myname1, myextension, target_file, mycontent)
my makefile(myname2, myextension, target_file, mycontent)

--done
display dialog "Nib unlocked." buttons {"Open", "Done"} default button 2 with icon 1
set button_returned to button returned of the result
if button_returned is "Open" then
	
	--open
	tell application "Finder"
		open target_file
	end tell
end if

--***** END *****

It’s perfectly logical.

The Finder (as well as System Events) can create a new file in a package but you can’t do that into a flat file.

Yvan KOENIG (VALLAURIS, France) mercredi 10 septembre 2014 09:15:27

I can’t do it into an NON-flat file.

choose folder version: Finder greys out the nib on choose folder.

choose file version: Finder returns an error
Error: -1700. Finder got an error: Can’t make alias “*Macintosh:Users:alnoor:Desktop:BasicWindows.nib:” into type folder.

The address shows a colon at the end, so it’s a package, but still fails ??

I don’t understand.

set targetNib to choose file of type {"com.apple.interfacebuilder.document"}

--> alias "<myHD>:Users:<myAccount>:Desktop:Contents:Resources:MainMenu.nib:"

set file1 to (path to desktop as text) & "whyNot.nib"
do shell script "mv " & quoted form of POSIX path of file1 & space & quoted form of POSIX path of targetNib as text with administrator privileges

As you may see above, choose file returned the wanted alias.
And now, the nib contents is :
designable.nib
keyedobjects.nib
whyNot.nib ← ADDED

Yvan KOENIG (VALLAURIS, France) mercredi 10 septembre 2014 19:20:37

Yes, this does work!

But ONLY if I have Interface Builder.app present on the external drive. If not, I cannot choose the nib file (in your example MainMenu.nib) in the choose dialog. It’s greyed out & the nib’s icon is a folder, not a document file.

Do you have Interface Builder.app installed?
I’m curious as to why the nib file should be treated differently with or without it.
Like I say, on my system: a nib file appears as a regular folder without IB present, and as an interface builder document with icon if IB is present.

In any case, this is certainly a workable solution, as I would only use the script in conjunction with IB.

I’m not a developer. Xcode is installed on my machine but I rarely open it.
Xcode export to the system 82 UTTypeIdentifiers and one of them is com.apple.interfacebuilder.document.

So, as far as I know, running it exports the Unique Type Identifier so that file info and System Events recognize it.
As far as I know, the Finder doesn’t know or at least doesn’t report files type identifiers.

Try to run :

set aNib to choose file of type {"nib"}
--> alias "<myHD>:Users:<myAccount>:Desktop:Contents:Resources:MainMenu.nib:"
log type identifier of (get info for aNib)

tell application "System Events"
	log (get type identifier of aNib)
	(*com.apple.interfacebuilder.document*)
end tell

tell application "Finder"
	properties of aNib
	--> {class:document file, name:"MainMenu.nib", index:26, displayed name:"MainMenu.nib", name extension:"nib", extension hidden:false, container:folder "Resources" of folder "Contents" of folder "Desktop" of folder "<myAccount>" of folder "Users" of startup disk, disk:startup disk, position:{-1, -1}, desktop position:missing value, bounds:{-33, -33, 31, 31}, kind:"Interface Builder NIB Document", label index:0, locked:false, description:missing value, comment:"", size:10777, physical size:20480, creation date:date "samedi 17 mars 2012 10:27:52", modification date:date "mercredi 10 septembre 2014 19:18:04", icon:missing value, URL:"file:///Users/<myAccount>/Desktop/Contents/Resources/MainMenu.nib", owner:"<myAccount>", group:"(inconnu)", owner privileges:read write, group privileges:read only, everyones privileges:read only, file type:missing value, creator type:missing value, stationery:false, product version:"", version:""}
end tell

If you get something like :
type identifier:“dyn.age80e2pp”
it’s that the Unique Type Identifier was not exported on your machine.
In such case, use

set aNib to choose file of type {"nib"}

in your script.

Yvan KOENIG (VALLAURIS, France) mercredi 10 septembre 2014 21:20:32

thanks.
this works:

choose file of type {"com.apple.interfacebuilder.document"}

It’s what I urged you to use in my messages #4, #6 and #9.

Given what you wrote since, I assumed that this UTI was not available on your system ” which normally means that the application Xcode, or InterfaceBuilder was never used on it.

Yvan KOENIG (VALLAURIS, France) mardi 16 septembre 2014 10:10:18