Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

TheEmpty

macrumors member
Original poster
Jul 11, 2011
88
0
NYC
Today I'm having a mac day, played around with xCode, remembered why I love Ruby and Java and don't like to journey down into lower levels. Then I found AppleScript, which is really nice! So I'm trying to make a script that will package my Java applications for school. I seem to be having a problem when telling pages to open the document.

Q0DFn.png


PHP:
set lastName to "SET ME"
set studentID to "SET ME TOO"
set projectName to text returned of (display dialog "Project Name" default answer "UniProject")
set projectNumber to text returned of (display dialog "Project Number" default answer 1)

-- setup the tmp directory
do shell script "rm -fr /tmp/uni-packager"
do shell script "mkdir /tmp/uni-packager"

-- copy the Netbean files
do shell script "cp -r ~/NetBeansProjects/" & projectName & "/ /tmp/uni-packager/project"

-- look for pages doc and compile
set fileLocation to "~/Documents/" & projectName & ".pages"
set pdfLocation to "~/Desktop" & projectName & ".pdf"
tell application "Pages"
	open file fileLocation
	set product to front document
	save product in pdfLocation
end tell
do shell script "mv " & pdfLocation & " /tmp/uni-packager/"
-- do shell script "cp ~/Documents"

-- remove git and mac files
do shell script "rm -r /tmp/uni-packager/project/.git*"
do shell script "find /tmp/uni-packager/. -name *.DS_Store -type f -exec rm {} \\;"
-- compress
do shell script "zip -r ~/Desktop/" & lastName & "_" & studentID & "_project" & projectNumber & ".zip /tmp/uni-packager/*"
-- delete temp
do shell script "rm -rf /tmp/uni-packager"
 
Today I'm having a mac day, played around with xCode, remembered why I love Ruby and Java and don't like to journey down into lower levels. Then I found AppleScript, which is really nice! So I'm trying to make a script that will package my Java applications for school. I seem to be having a problem when telling pages to open the document.

Image

PHP:
set lastName to "SET ME"
set studentID to "SET ME TOO"
set projectName to text returned of (display dialog "Project Name" default answer "UniProject")
set projectNumber to text returned of (display dialog "Project Number" default answer 1)

-- setup the tmp directory
do shell script "rm -fr /tmp/uni-packager"
do shell script "mkdir /tmp/uni-packager"

-- copy the Netbean files
do shell script "cp -r ~/NetBeansProjects/" & projectName & "/ /tmp/uni-packager/project"

-- look for pages doc and compile
set fileLocation to "~/Documents/" & projectName & ".pages"
set pdfLocation to "~/Desktop" & projectName & ".pdf"
tell application "Pages"
	open file fileLocation
	set product to front document
	save product in pdfLocation
end tell
do shell script "mv " & pdfLocation & " /tmp/uni-packager/"
-- do shell script "cp ~/Documents"

-- remove git and mac files
do shell script "rm -r /tmp/uni-packager/project/.git*"
do shell script "find /tmp/uni-packager/. -name *.DS_Store -type f -exec rm {} \\;"
-- compress
do shell script "zip -r ~/Desktop/" & lastName & "_" & studentID & "_project" & projectNumber & ".zip /tmp/uni-packager/*"
-- delete temp
do shell script "rm -rf /tmp/uni-packager"

First of all, Applescript does not expand "~" when used outside of "do shell script", so you have add:
Code:
set homeFolder to POSIX path of home folder from user domain
Also, you can't use POSIX paths in regular applescript. You have to replace "open file fileLocation" with "open POSIX file fileLocation", after you replace the "~" with the "homeFolder" variable up there. You're missing a "/" in pdflocation too. You also have to put the "rm" in a try block incase the folder doesn't exist already, which will be true by default. This code has so many problems...
 
Last edited:
First of all, Applescript does not expand "~" when used outside of "do shell script", so you have add:
Code:
set homeFolder to POSIX path of home folder from user domain
Also, you can't use POSIX paths in regular applescript. You have to replace "open file fileLocation" with "open POSIX file fileLocation", after you replace the "~" with the "homeFolder" variable up there. You're missing a "/" in pdflocation too. You also have to put the "rm" in a try block incase the folder doesn't exist already, which will be true by default. This code has so many problems...
Thanks for the POSIX stuff, it's actually harder for me to do it in this then in something like C :p

rm doesn't need a try block because of the options ;)

still having a bit of trouble, think I'll just end up doing it by hand.
zwKq7.png

just ./ it to show it's there
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.