1 %GHOSTSCRIPT Calls a local GhostScript executable with the input command
4 % [status result] = ghostscript(cmd)
6 % Attempts to locate a ghostscript executable,
finally asking the user to
7 % specify the directory ghostcript was installed into. The resulting path
8 % is stored
for future reference.
10 % Once found, the executable is called with the input command
string.
12 % This
function requires that you have Ghostscript installed on your
13 % system. You can download
this from: http:
16 % cmd - Command
string to be passed into ghostscript.
19 % status - 0 iff command ran without problem.
20 % result - Output from ghostscript.
22 % Copyright: Oliver Woodford, 2009-2013
24 % Thanks to Jonas Dorn
for the fix
for the title of the uigetdir window on
26 % Thanks to Nathan Childress
for the fix to the
default location on 64-bit
28 % 27/4/11 - Find 64-bit Ghostscript on Windows. Thanks to Paul Durack and
29 % Shaun Kline
for pointing out the issue
30 % 4/5/11 - Thanks to David Chorlian
for pointing out an alternative
31 % location
for gs on linux.
32 % 12/12/12 - Add extra executable name on Windows. Thanks to Ratish
33 % Punnoose
for highlighting the issue.
34 % 28/6/13 - Fix error
using GS 9.07 in Linux. Many thanks to Jannick
35 % Steinbring
for proposing the fix.
36 % 24/10/13 - Fix error
using GS 9.07 in Linux. Many thanks to Johannes
38 % 23/01/2014 - Add full path to ghostscript.txt in warning. Thanks to Koen
39 % Vermeer
for raising the issue.
41 function varargout = ghostscript(cmd)
42 % Initialize any required system calls before calling ghostscript
45 shell_cmd =
'export LD_LIBRARY_PATH=""; '; % Avoids an error on Linux with GS 9.07
48 shell_cmd =
'export DYLD_LIBRARY_PATH=""; '; % Avoids an error on Mac with GS 9.07
51 [varargout{1:nargout}] = system(sprintf(
'%s"%s" %s', shell_cmd, gs_path, cmd));
54 function path_ = gs_path
56 % Start with the currently
set path
57 path_ = user_string(
'ghostscript');
58 % Check the path works
59 if check_gs_path(path_)
62 % Check whether the binary is on the path
64 bin = {
'gswin32c.exe',
'gswin64c.exe',
'gs'};
70 if check_store_gs_path(path_)
74 % Search the obvious places
76 default_location = 'C:\Program Files\gs\';
77 dir_list = dir(default_location);
79 default_location = 'C:\Program Files (x86)\gs\'; % Possible location on 64-bit systems
80 dir_list = dir(default_location);
82 executable = {
'\bin\gswin32c.exe',
'\bin\gswin64c.exe'};
84 % If there are multiple versions, use the newest
85 for a = 1:numel(dir_list)
86 ver_num2 = sscanf(dir_list(a).name, 'gs%g');
87 if ~isempty(ver_num2) && ver_num2 > ver_num
88 for b = 1:numel(executable)
89 path2 = [default_location dir_list(a).name executable{b}];
90 if exist(path2,
'file') == 2
97 if check_store_gs_path(path_)
101 executable = {
'/usr/bin/gs',
'/usr/local/bin/gs'};
102 for a = 1:numel(executable)
103 path_ = executable{a};
104 if check_store_gs_path(path_)
109 % Ask the user to enter the path
111 if strncmp(computer, 'MAC', 3) % Is a Mac
112 % Give separate warning as the uigetdir dialogue box doesn't have a
114 uiwait(warndlg('Ghostscript not found. Please locate the program.'))
116 base = uigetdir('/', 'Ghostcript not found. Please locate the program.');
118 % User hit cancel or closed window
121 base = [base filesep];
122 bin_dir = {
'', [
'bin' filesep], [
'lib' filesep]};
123 for a = 1:numel(bin_dir)
125 path_ = [base bin_dir{a} bin{b}];
126 if exist(path_,
'file') == 2
127 if check_store_gs_path(path_)
134 error('Ghostscript not found. Have you installed it from www.ghostscript.com?');
136 function good = check_store_gs_path(path_)
137 % Check the path is valid
138 good = check_gs_path(path_);
142 % Update the current default path to the path found
143 if ~user_string('ghostscript', path_)
144 warning('Path to ghostscript installation could not be saved. Enter it manually in %s.', fullfile(fileparts(which('user_string.m')), '.ignore', 'ghostscript.txt'));
149 function good = check_gs_path(path_)
150 % Check the path is valid
153 shell_cmd = 'export DYLD_LIBRARY_PATH=""; '; % Avoids an error on Mac with GS 9.07
155 [good, message] = system(sprintf('%s"%s" -h', shell_cmd, path_));