|
|
10.6: Create a new file anywhere via an Automator Service
Hi, I am very new to AppleScript, the original script in this thread is one of the first few that I tried. I have extended the code to check if the file already exists, and if it exists the file name is changed. Also, if the user puts a different name for the file, the script remembers that and next time shows that title by default. I could not create the contextual menu for the blank space in finder window though. Here is the code:
property defaultFileName : "newTextFile"
on run {input, parameters}
try
tell application "Finder" to set the sourceFolder to (folder of the front window) as alias
on error
-- no open folder windows
set the sourceFolder to path to desktop folder as alias
end try
set flagFileExists to true
set indexFile to ""
repeat while flagFileExists
set indexFile to (indexFile + 1)
tell application "Finder"
set flagFileExists to (exists file (defaultFileName & indexFile & ".txt") in sourceFolder)
end tell
end repeat
try
tell me
activate
set newFileName to text returned of (display dialog "Enter new file name" default answer "" & defaultFileName & indexFile)
end tell
if newFileName is not equal to "" & defaultFileName & indexFile then
set defaultFileName to newFileName
end if
set newFile to "" & sourceFolder & newFileName & ".txt"
if not flagFileExists then
set touchScript to "touch " & quoted form of (POSIX path of newFile)
set openScript to "open " & quoted form of (POSIX path of newFile)
do shell script touchScript
do shell script openScript
else
display dialog "File already exists"
end if
end try
return input
end run
|
SearchFrom our Sponsor...Latest Mountain Lion HintsWhat's New:HintsNo new hintsComments last 2 daysLinks last 2 weeksNo recent new linksWhat's New in the Forums?
Hints by TopicNews from Macworld
From Our Sponsors |
|
Copyright © 2014 IDG Consumer & SMB (Privacy Policy) Contact Us All trademarks and copyrights on this page are owned by their respective owners. |
Visit other IDG sites: |
|
|
|
Created this page in 0.09 seconds |
|