Jump to content

New to scripting, could use some direction.


Recommended Posts

Hello Everyone,

 

I'm hoping to find some direction here, so let me be clear. This is not a "please, oh please, do this for me" post. I genuinely want to learn.

 

 

To the point:

 

I've created a workflow for Entropy using applescript, and I have two ideas I wish to implement. Being that I'm very new to all of this (scripting in general), I don't have the slightest clue where I should be focusing my attention (e.g. do I need php, perl, etc?), or can I get to where I want without such methods.

 

Here's what I want to add:

 

1. Currently I'm calling the script (run script; /usr/bin/osascript) with a keyword (zip), and using the query to designate the name of the archive, if no query is entered the script uses a default name. When I want to add a password to the archive I've set up another run script which uses the same keyword, but uses the modifier key cmd. This runs an almost identical script, but using applescripts display dialog to set the password. 

 

I wish to forgo the display dialog in applescript, and split the alfred query so that when I want to add a name and a password I would enter something like "zip name:password." How can I do this?

 

 

2. It seems that scripts called with a keyword work as expected, but if want to use a file action then I have to first open the selection in finder before I use the applescript. Why is this, and how can I get around this?

 

 

Applescript:

 

tell application "Finder" to set theSel to selection as alias list

 

set theQ to "{query}" -- Alfred Query (Archive Name)

 

 

if (count of theSel) is 1 then

if theQ is "" then

if (theSel as string) ends with ":" then

set folderName to the POSIX path of theSel

set thePath to text 1 thru ((offset of ":" in folderName) - 2) of folderName & ".zip"

tell application "System Events"

if (exists file (thePath)) then

display dialog ("Archive Exists")

return

end if

end tell

else

set shortName to the POSIX path of theSel

set thePath to text 1 thru ((offset of "." in shortName) - 1) of shortName & ".zip"

tell application "System Events"

if (exists file (thePath)) then

display dialog ("Archive Exists")

return

end if

end tell

end if

else

set thePath to ((path to desktop from user domain) as string) & theQ & ".zip"

tell application "System Events"

if (exists file (thePath)) then

display dialog ("Archive Exists")

return

end if

end tell

end if

else

repeat with i from 1 to count of theSel

set item i of theSel to POSIX path of item i of theSel

end repeat

set archiveName to getEmptyPath(path to desktop as text, "archive", "zip")

if theQ is "" then

set thePath to ((path to desktop from user domain) as string) & archiveName & ".zip"

else

set thePath to ((path to desktop from user domain) as string) & theQ & ".zip"

end if

end if

 

 

-- Set Password for Archive

--set thePassword to the text returned of (display dialog "Enter Password" default answer "")

 

 

tell application "Entropy"

archive thePath files theSel --settings {password:thePassword, encryption method:0}

end tell

 

 

---------------------------------------------------------------

-- See if a file with the passed name exists

-- If so, add an integer to the name, and check again

-- Keep checking until an unused name is found

-- Parameters:    path of file's container

--                file name, no extension

--                file extension

-- Returns:       updated file name

---------------------------------------------------------------

 

on getEmptyPath(containerPath, fileName, fileExtension)

set filePath to containerPath & fileName

tell application "Finder"

if exists file (filePath & "." & fileExtension) then

set x to 1 -- Start Counting

repeat

set cFilePath to filePath & " " & x & "." & fileExtension

if exists file cFilePath then -- Are we There?

set x to x + 1 -- No, Increment Counter

else

exit repeat -- Yes, Leave

end if

end repeat

return fileName & " " & x -- This Name is Safe to Use

else

return fileName -- Use Original Name

end if

end tell

end getEmptyPath

Link to comment

You can parse the query with a delimiter e.g.

-- {query} format is name:password
set sArgv to "{query}" as text
if sArgv contains ":" then
	set text item delimiters to {":"}
	set ZipName to text item 1 of sArgv
	set ZipPassword to text item 2 of sArgv
	set text item delimiters to ""
else
	set ZipName to sArgv
end if

For file action take a look at my Zip workflow (not updated to the latest Alfred version yet): link

Link to comment

@@Carlos-Sz

 

Well... The first part was easier than I expected. The file action bit is taking me for a ride. Recreating your method in my script is not working for me at the moment (but I'll figure it out eventually), however, clipping out your last section, where you save via "do shell script," and replacing with the Entropy create archive works beautifully. (It just blatantly rips off your work).

 

I have one more question before I move along. Entropy has a inspect command that lists archive contents without unarchiving. If I wanted to take the result from an applescript and display it in the search results area of Alfred where can I look for examples?

Link to comment

@@Carlos-Sz

 

Well... The first part was easier than I expected. The file action bit is taking me for a ride. Recreating your method in my script is not working for me at the moment (but I'll figure it out eventually), however, clipping out your last section, where you save via "do shell script," and replacing with the Entropy create archive works beautifully. (It just blatantly rips off your work).

 

I have one more question before I move along. Entropy has a inspect command that lists archive contents without unarchiving. If I wanted to take the result from an applescript and display it in the search results area of Alfred where can I look for examples?

 

Great.

 

Entropy should return a list of files then:

-- set sFilesInZip to "app command to inspect"
repeat with sCur in sFilesInZip
	-- add an item (sCur) in Alfred here
end repeat
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...